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!

If you like to try it out you can find the library here: https://github.com/squix78/json-streaming-parser. In theory it should work for any Arduino target device but I only tested it with the ESP8266.

Happy hacking!

Posted by Daniel Eichhorn

Daniel Eichhorn is a software engineer and an enthusiastic maker. He loves working on projects related to the Internet of Things, electronics, and embedded software. He owns two 3D printers: a Creality Ender 3 V2 and an Elegoo Mars 3. In 2018, he co-founded ThingPulse along with Marcel Stör. Together, they develop IoT hardware and distribute it to various locations around the world.

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 to ManfredCancel reply