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

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!

Related:  Live reload with appengine-maven-plugin

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!

Now a beer! Did you like this post? It often takes me several hours of my free time to write one. If I was your neighbour would you offer me a beer for the hard work I did for you? The beauty is: beers can be teletransported with a painless donation using Paypal. A beer in a Swiss bar costs about USD $4.80. Or use this affiliate link if you order something on Banggood and I'll get a small kickback. Thank you!