JSON to C Struct Generator

Generate C struct definitions with typedef from JSON. Supports static and dynamic memory modes, nested structs, arrays, and optional json-c library integration.

Memory:

What is JSON to C Struct Generator?

This tool converts JSON data into C struct definitions with typedef declarations. It automatically infers C types from JSON values, converts field names to snake_case (C convention), handles nested structs, arrays with count fields, and generates ready-to-use C header code.

How to Use

  1. Set the root struct name and optional struct name prefix
  2. Choose memory mode: static (fixed-size arrays/char buffers) or dynamic (pointers with malloc/free)
  3. Optionally enable json-c library support for parse/serialize function stubs
  4. Paste your JSON data into the input area and click "Generate C Structs"

Why Use This Tool?

Automatically infer C types from JSON values
Convert field names to snake_case following C conventions
Generate nested structs with correct ordering (children first)
Static mode: fixed-size char arrays and fixed-size arrays with count fields
Dynamic mode: char* pointers with create/free helper functions
Optional json-c library integration with parse and serialize stubs

Tips & Best Practices

  • Use realistic sample data — type inference depends on actual values
  • Static mode is simpler and avoids memory management; dynamic mode is more flexible
  • The struct prefix is prepended to nested object struct names for namespace isolation
  • Arrays of primitives generate wrapper structs with items array and count field
  • Boolean values are mapped to int (0/1) since C has no native bool type without stdbool.h

Frequently Asked Questions

How are JSON types mapped to C?

JSON strings → char* (dynamic) or char[256] (static), integers → int, decimals → double, booleans → int (0/1), null → void*, arrays → wrapper struct with items array + count field, objects → nested typedef struct.

What is the difference between static and dynamic memory mode?

Static mode uses fixed-size char arrays (char[256]) and fixed-size arrays with a count field. Dynamic mode uses char* pointers and generates create() and free() helper functions that handle malloc and free for proper memory management.

What does the json-c support option do?

When enabled, the tool includes json-c headers and generates from_json() and to_json() function stubs for each struct. These functions parse json_object into your struct and serialize your struct back to json_object using the json-c library API.

Why are arrays wrapped in separate structs?

C does not have dynamic arrays built-in. To represent a JSON array, we create a wrapper struct containing the array data and a count field. This allows you to know how many items are in the array at runtime.

Is my data sent to a server?

No, all processing happens entirely in your browser. Your JSON data never leaves your device.

Related Tools