Advertisement

JSON to C Struct

Convert JSON into C structs. Strings become char buffers, numbers become int or double, arrays become pointers, and nested structs are declared in order.

JSON input

Paste JSON, optionally name the root type, then generate.

Your C struct code will appear here.
Advertisement

C friendly data layouts

C needs fixed memory layouts, so the generator maps JSON's dynamic types onto buffers, ints, doubles, and pointers. The result is a plain old data struct you can memcpy, serialize, or send over a socket.

Tune buffers to your data

The default char buffer sizes are starting points. Measure your longest real strings and shrink the buffers to match, which matters on memory constrained embedded targets.

JSON to C Struct FAQs

How are strings represented?
JSON strings become fixed char buffers like char name[256]. Adjust the size to your data; the generator picks a safe default, not a measured maximum.
How are nested objects handled?
Each nested object becomes its own typedef struct declared before the parent, so the hierarchy compiles in dependency order.
How are arrays represented?
Arrays become typed pointers like int *scores with a comment noting the array origin. C has no built in dynamic arrays, so pair the pointer with a count field in your code.
Which headers are included?
The output includes stdbool.h for bool fields and string.h where useful, so the struct compiles as emitted in C99 and later.
How do I parse JSON into this?
Use a C JSON library like cJSON or jansson to parse, then copy values into the struct fields manually or with a small mapping function.
Advertisement