JSON to Code

JSON to C, with cJSON

Paste a JSON sample. Get C, with cJSON types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with C, with cJSON chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this C, with cJSON:

/**
 * stdout
 * This file has been autogenerated using quicktype https://github.com/quicktype/quicktype - DO NOT EDIT
 * This file depends of https://github.com/DaveGamble/cJSON, https://github.com/joelguittet/c-list and https://github.com/joelguittet/c-hashtable
 * To parse json data from json string use the following: struct <type> * data = cJSON_Parse<type>(<string>);
 * To get json data from cJSON object use the following: struct <type> * data = cJSON_Get<type>Value(<cjson>);
 * To get cJSON object from json data use the following: cJSON * cjson = cJSON_Create<type>(<data>);
 * To print json string from json data use the following: char * string = cJSON_Print<type>(<data>);
 * To delete json data use the following: cJSON_Delete<type>(<data>);
 */

#ifndef __STDOUT__
#define __STDOUT__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <cJSON.h>
#include <hashtable.h>
#include <list.h>

#ifndef cJSON_Bool
#define cJSON_Bool (cJSON_True | cJSON_False)
#endif
#ifndef cJSON_Map
#define cJSON_Map (1 << 16)
#endif
#ifndef cJSON_Enum
#define cJSON_Enum (1 << 17)
#endif

struct Address {
    char * city;
    void * postcode;
};

struct Root {
    int64_t id;
    char * name;
    bool active;
    char * joined;
    list_t * tags;
    struct Address * address;
    list_t * scores;
};

and 258 more lines. Open the tool to see the whole file.

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

/**
 * stdout
 * This file has been autogenerated using quicktype https://github.com/quicktype/quicktype - DO NOT EDIT
 * This file depends of https://github.com/DaveGamble/cJSON, https://github.com/joelguittet/c-list and https://github.com/joelguittet/c-hashtable
 * To parse json data from json string use the following: struct <type> * data = cJSON_Parse<type>(<string>);
 * To get json data from cJSON object use the following: struct <type> * data = cJSON_Get<type>Value(<cjson>);
 * To get cJSON object from json data use the following: cJSON * cjson = cJSON_Create<type>(<data>);
 * To print json string from json data use the following: char * string = cJSON_Print<type>(<data>);
 * To delete json data use the following: cJSON_Delete<type>(<data>);
 */

#ifndef __STDOUT__
#define __STDOUT__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <cJSON.h>
#include <hashtable.h>
#include <list.h>

#ifndef cJSON_Bool
#define cJSON_Bool (cJSON_True | cJSON_False)
#endif
#ifndef cJSON_Map
#define cJSON_Map (1 << 16)
#endif
#ifndef cJSON_Enum
#define cJSON_Enum (1 << 17)
#endif

struct Address {
    char * city;
    void * postcode;
};

struct Root {
    int64_t id;
    char * name;
    bool active;

and 263 more lines. Open the tool to see the whole file.

What you need in your project

cJSON.

Plain C has no way to describe a shape, so the output is longer than the rest: a struct, a parse function and a free function for each type.

Every parse function returns a pointer you must free. The file writes the free function for you.

The trap to know about

Forgetting to free is the fault that bites. Each type has its own free function, and a free of the top type frees what it holds.

The options you can change

These belong to C, with cJSON, so the row of switches changes with the language.

OptionWhat it doesDefault
Hash table sizeHashtable size, used when maps are created (64 by default)64
Type namingNaming style for typespascal-case | underscore-case | camel-case | upper-underscore-case | pascal-case-upper-acronyms | camel-case-upper-acronyms
Member namingNaming style for memberspascal-case | underscore-case | camel-case | upper-underscore-case | pascal-case-upper-acronyms | camel-case-upper-acronyms
Enum namingNaming style for enumeratorspascal-case | underscore-case | camel-case | upper-underscore-case | pascal-case-upper-acronyms | camel-case-upper-acronyms

Questions

How do I turn JSON into C, with cJSON?

Paste your JSON into the box, choose C, with cJSON, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my C, with cJSON project?

cJSON.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to C++ JSON to Crystal All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code