ESP8266/Arduino: processing huge JSON objects directly on the ESP

Do you like this post? Writing posts, developing libraries and programming online font creators is actually hard work. I order most of my electronics from a Chinese website called Banggood. It is save and the items usually arrive in good quality. With every order you do there by following this link you are supporting the blog. Thank you!

For a while now I wanted to improve my weather station project by adding more sources of information that can be displayed. But these sources I had in mind either used SSL (https) or delivered huge JSON responses. SSL is not yet supported on the ESP8266/Arduino platform (and from what I hear also broken on Nodemcu/Lua). Big JSON objects are a problem because all the existing libraries I tried are DOM based which consumes a lot of memory since they keep the whole JSON object in memory (at least once).

So I decided to tackle one problem at a time and looked at the big JSON object issue first. If you want to parse big documents you can either build a representation of the object in memory (DOM) or you can write a event based parser. This means that your parser is processing the document as it comes in as a stream, character by character and that it will notify you with different event types once portions of the document were parsed successfully. This is very helpful since you don’t have to keep the whole  document in memory and you can even start parsing the stream almost immediately. You might be familiar with the SAX parser for XML which also follows this concept.

This concept is very useful on devices with little memory as the ESP8266. Looking for a start I found such a stream based JSON parser written in PHP and decided to port it to C++.

The result looks very promising: I wrote a little example program for the ESP8266 which downloads a sample JSON object of 1MB size. In the sketch I’m only interested in one of the 5000 sub-objects and print it out as soon as I see it. During processing I print out the heap size with ESP.getFreeHeap() and the available heap size remains stable at around 32KB!

If you like to try it out you can find the library here: In theory it should work for any Arduino target device but I only tested it with the ESP8266.

Happy hacking!

Posted by squix78

One comment

  1. Thank you for providing the parser. I have nothing seen similar before.
    I have tested the software within a server running on ESP. But I have a problem: only one string is being processed. After the end of document is reached no further is string is parsed.
    Shouldn’t it be necessarry to set the state machine to start values again?

Leave a Reply