I recently had to parse Json data on a small embedded system. When looking for tools to help with this, I found and loved Serge Zaitsev’s jsmn (pronounced “Jasmine”).
Jsmn is written in C and considered one if not the most efficient Json parser in the field. Its philosophy is to be as simple as possible: no dynamic memory allocation, no callbacks, and absolutely no dependences. It’s a great piece of software, but it is a bit light on examples especially for parsing bigger json data structures. I got stuck and asked a question on how others do structure their parsers but so far nobody shared a working solution.
I needed to split the Json parser into multiple functions. Mostly because I do not like big junks of code and I need to be able to unit-test smaller pieces of my code like the specialized parser functions.
Some sample Json data to parse:
I studied all jsmn samples on the web I could find but did not find a simple solution I liked so I had to come up with something myself. The solution I came up with is really, really simple - in the past this almost always proved to be a reliable indicator for a good solution. So I wrote this post to share it with you.
First I parse the Json data into an array of tokens:
Now I process the array with specialized parser functions like here:
Each parser function advances the index i into the tokens array. Since C has no pass-by-reference I need to hand in a pointer to i.
The test case prints out a lot of information so it is easy to see what is going on from the output of running the test case on esp32:
I commited the complete sample code to my github - feel free to take a look.
I hope this post is helpful to you.
All the best, Mark