![NOTE] > This library should not be used for anything illegal if you use this library you aknowledge the fact that I am not responsible for your own personal stupidity.
A Minimal JSON parser in C. This project was built part of maldev academy training that I am curretnly doing this is a section of a bigger project I am working on that I will release soon! The whole philosophy around this parser was the following:
- Parse JSON easily
- Query directly the JSON in C to get the values
- Does not break on malformed data
- No external dependencies (no CRT on windows, stdlib on linux)
- Cross platform -> made in pure C
For the malformed data point the whole idea was what if there is issues in getting the json data for example I receive {"token":1234 here it would be invalid json right the closing curly brace is missing. But if we look at the code well token is there why would the parser not work here? why should the gramar be strict? so in this parser this is still considered valid json and is parsed. What about garbage{"token":1234 well the parser would just skip garbage and still parse the following characters: {, ", [ The rest is irrelevant for this parser. We just need the : to distinguish a key value pair obviously.
The JSON is placed inside of the following BST:
struct json_tree {
struct json_tree *right;
struct json_tree *left;
struct json_tree *parent;
struct json_node *node;
};Then each node is memory compact using union and has the following data:
struct json_node {
int type;
char *key;
union value {
int integer;
int boolean;
char *string;
struct json_tree *object;
struct array {
struct json_node **items;
unsigned int count;
} array;
} value;
};Provided is a example of what a tree would look like for a simple json example:

Here are the calls that you can use to work with the json tree:
// Tree creation and deletion
struct json_tree *json_to_tree(char const *);
void free_tree(struct json_tree *);
// set / edit / insert data
struct json_node *json_get_data(struct json_tree *, char const *);
struct json_node *json_set_data(struct json_tree *, char const *, void *, int, unsigned int);
struct json_node *json_insert_data(struct json_tree *, char const *, void *, int, unsigned int);
// serializer
char *tree_to_json(struct json_tree *);They are all self explanatory good luck! Only thing to know is that on json_set_data and json_insert_data if you are NOT parsing an array as a argument then you should put 0 as then last argument.
The library can be used like so:
struct json_tree *json = 0;
struct json_node *token = 0;
char str[] = "{\"token\":\"aBcDeF\", \"num\":1234}";
char *ptr = my_strdup("coucou");
char *serialized = 0;
json = json_to_tree(str); // create the tree
token = json_get_data(json, "token"); // get the data out
if (token == 0)
// error detection here...
token->value.string; // use the actual value
json_set_data(json, "token", ptr, JSON_STRING, 0); // important: here last argument is used for array lengths only...
my_free(ptr);
serialized = tree_to_json(json); // take the edited tree and put it back into json..
free_tree(json);
my_printf("%s\n", serialized);
my_free(serialized);