jsmn – faster, lighter JSON parser in C

jsmn (pronounced like ‘jasmine’) is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects.

You may read our another post “What is JSON and Understanding JSON syntax with simple example” to basics of JSON.

This post gives the details about how we can use jsmn to parse the JSON and create a structure from it.

$ git clone https://github.com/zserge/jsmn.git
$ cd jsmn

This directory also contains an example which shows how to do the initialisation required to parse the JSON and also parses following JSON,

{"user": "johndoe", "admin": false, "uid": 1000, "groups": ["users", "wheel", "audio", "video"]}
$ make simple_example

Execute this simple_example as,

$ ./simple_example 
- User: johndoe
- Admin: false
- UID: 1000
- Groups:
  * users
  * wheel
  * audio
  * video

As we can see in this example, all the initalisation API’s etc are written in jsmn.h header file, hence when we want to write our own parser code, we just need to include this header in our code and call the initialisation code.. As shown in below example of initialisation code,

#include "jsmn.h"

...
jsmn_parser p;
jsmntok_t t[128]; /* We expect no more than 128 JSON tokens */

jsmn_init(&p);
r = jsmn_parse(&p, s, strlen(s), t, 128);

Reference – https://github.com/zserge/jsmn

1 thought on “jsmn – faster, lighter JSON parser in C”

Leave a Comment