Loading JSON to C Converter...
Please wait a moment

How to Turn JSON into C Structs

Start from a real JSON sample and you get struct definitions plus the cJSON code that fills them, ready to drop next to cJSON.c.

Step 1

Input Your JSON Data

Start by adding your JSON data that needs to be converted to C structs. Every object in the sample becomes a struct, so paste data that shows all the fields you expect. If you paste a list, every item is looked at, not just the first.

Example: Try This JSON Data

Copy and paste this JSON example to see how it works:

{
  "server": {
    "id": 42,
    "name": "web-server-01",
    "hostname": "api.example.com",
    "port": 8080,
    "isActive": true,
    "tags": [
      "production",
      "api",
      "critical"
    ],
    "config": {
      "maxConnections": 1000,
      "timeout": 30,
      "enableLogging": true
    }
  }
}
Step 2

Review Generated C Structs

The output starts with the struct definitions. A short comment at the top lists anything worth knowing about your data, such as IDs too long for cJSON to read exactly.

•
Struct definitions: one struct per JSON object, each with a typedef so you can write Server * instead of struct Server *
•
Types from your data: int, int64_t, uint64_t, double, bool and char *, picked from how each value is written (see the C type system)
•
Nested objects: each gets its own struct and is stored as a pointer, NULL when it is missing
•
Lists: a pointer plus a size_t _count member, allocated with malloc while reading
Step 3

Use the Reading and Free Functions

The rest of the file reads the JSON with the cJSON library. Add cJSON.c and cJSON.h to your project and build both files together, for example gcc -std=c99 -Wall converted_data.c cJSON.c.

C Code Output

The Step 1 JSON gives these structs (the reading code in the middle is left out here):

typedef struct Root Root;
typedef struct Server Server;
typedef struct Config Config;

struct Root {
    Server *server;
};

struct Server {
    int id;
    char *name;
    char *hostname;
    int port;
    bool is_active; /* "isActive" */
    char **tags;
    size_t tags_count;
    Config *config;
};

struct Config {
    int max_connections; /* "maxConnections" */
    int timeout;
    bool enable_logging; /* "enableLogging" */
};

/* ...reading code... */

Root *parse_root(const char *json_text);
void free_root(Root *obj);

And this is how you would use them:

Root *data = parse_root(json_text);
if (data && data->server) {
    printf("%s:%d
", data->server->hostname, data->server->port);
    for (size_t i = 0; i < data->server->tags_count; i++)
        printf("tag: %s
", data->server->tags[i]);
}
free_root(data);
Step 4

Copy or Download C Code

Copy the code or download it as a .c file. Rename the struct or the functions if they clash with names already in your project.

•
Copy to clipboard for immediate use in your C projects
•
Download .c file for integration into your codebase
•
Embedded targets - cJSON builds on small systems such as FreeRTOS and Zephyr, but the generated code uses malloc, so check your heap size
•
Desktop and server code - reading API responses or config files with the GNU C Library or any other C99 toolchain

What is JSON to C Conversion and Deserialization?

JSON to C conversion (also called JSON deserialization in C) transforms JSON data structures into C structs with proper type definitions, parsing functions, and memory management code. C has no built-in way to deserialize JSON, so you pick a library and write code that copies each value into your own structs. This page writes both parts for you from a sample: the structs and the code that fills them using cJSON, in plain C99 (ISO/IEC 9899).

Deserialization turns JSON text into C structs, and that is the direction this page covers; writing structs back out as JSON (serialization) is not generated. The output has the struct definitions, reading functions and free functions, and works in desktop programs as well as embedded projects on FreeRTOS, Zephyr, Mbed OS, and low-level programming projects compatible with GCC and Clang compilers. You can also deserialize JSON online, go the other way with C to JSON, or generate C++ instead with JSON to C++.

Frequently Asked Questions

Which JSON library does the generated C code use?

It uses cJSON, a small single-file C library (cJSON.c plus cJSON.h). Keys are looked up with cJSON_GetObjectItemCaseSensitive, so "id" and "Id" stay separate. The generated file compiles cleanly with gcc -std=c99 -Wall -Wextra -pedantic once cJSON.h is on the include path.

How are JSON numbers turned into C types?

The type comes from how each number is written in your JSON, across every item of a list. Whole numbers that fit in 32 bits become int, bigger ones int64_t (or uint64_t above the signed range), and anything with a decimal point or exponent, like 14.50 or 1e3, becomes double. A quoted value such as "00501" is a string in JSON, so it stays a char * and keeps its leading zeros.

Can cJSON read a long ID like 8901240544102066246 exactly?

No. cJSON stores every number as a double, which is exact only up to 9007199254740992. The member is still an int64_t, but cJSON hands back 8901240544102066176 for that ID. When your JSON holds values that large, the comment at the top of the output names the members affected. If you need every digit, send the ID as a JSON string or read it with a library that keeps 64-bit integers, such as jansson.

How does memory work in the generated code?

parse_root() allocates the top struct, copies every string with malloc, and allocates nested objects and lists. free_root() releases all of it, so call it once you are finished with the data. Lists are a pointer plus a matching _count member, which makes it easy to loop over them safely.

What happens to missing, null or mixed values?

A string, object or list that is missing stays NULL. A number or boolean that was missing or null in some of your items gets a has_ flag next to it, so you can tell 0 apart from "not there". A value that is sometimes a number and sometimes text, or a list of lists, is kept as a cJSON pointer you can inspect yourself.

What if a JSON key is not a valid C name?

Keys are turned into snake_case: firstName and first-name both become first_name, and the second one gets first_name_2 so nothing clashes. C keywords get an underscore (default_), keys that start with a digit get a field_ prefix, and accents are dropped. The original key is kept in a comment and is what the reading code looks up.

Is my JSON uploaded anywhere?

No. The C code is generated in your browser and the JSON is not sent to our server or kept anywhere; reloading the page starts with an empty editor.

Not sure your JSON is valid yet? Run it through the JSON validator first.