Parsing JSON Using C : Quick Introduction to JSMN Library

I was searching for a C library to parse a simple JSON file and after looking for a while I just found the right one!

As per title, the library goes by the name of JSMN which is pronounced ‘Jasmine’ and truly what the documentation says is correct. It is a minimalistic JSON parser written in pure C with no dependencies required. Goes without saying, you can use it on small embedded systems too.

It does exactly what it says. Let’s parse a simple JSON file in C using this library.

To begin with, I am going to create a Qt Project then add the library there. So this will be my project structure.

|------ Headers/
|----- jsmn.h
|------ Sources/
|----- jsmn.c
|----- main.c
|------ Other files
|----- test.json

You don’t need to structure the files in this way, let Qt handle it. :P

Creating a New Project in Qt Creator

Open up the best C/C++ IDE ever then create a new project from the option Non-Qt Project > Plain C Application.

Now you’re left with a simple main.c file along with some boring codes.

Adding the JSMN Library

You can clone the repository from here. Just copy the jsmn.h and jsmn.c file and paste it to the root directory of your newly created project folder.

To let the compiler know about the existence of the Jasmine library, you need to add the files to your project.

You can do it the nerd way or the easy way. Easy way is just to simply add the files by right clicking the project and use the Add Existing Files and select the jsmn.c and jsmn.h files.

Appropriate way: Go to the or the .pro file of your project then add the following lines.

SOURCES += main.c jsmn.c
HEADERS += jsmn.h

Now on saving the file the Qt will automatically add it and it will be visible on sidebar.

A Simple JSON File To Parse

No experiment can be done without a subject right? So I’ll use a simple JSON file to begin with. Here is the file,

You can either use this or use that one you like!

Adding Codes to `main.c` file for parsing the JSON file

The code is split in three sections, one function reads the given JSON file, stores it as a string. And another one is the parseJSON function which parses the file and the third one, callback function. You can send any callback function to process the parsed key-value pair.


You can find more examples in the official repository and here. And you’re always welcomed to ask questions to clear up any confusions.