Arduino library · Data and time

Json Streaming Parser: read JSON without holding it in RAM

Yes. You can use the Json Streaming Parser library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "Listening to a JSON document one character at a time", runs on the Arduino Uno R3.

Built in on Mokxi: #include <JsonStreamingParser.h> and it builds, with nothing to install. Free, and no account needed.

What Json Streaming Parser does

Most JSON libraries read the whole document into memory and then let you look around in it. A weather forecast or a list of transit times can be tens of kilobytes, which an Uno does not have and even an ESP8266 uses up fast. The Json Streaming Parser takes the opposite approach: you feed it one character at a time as it arrives from the network, and it tells you what it found as it goes.

You write a small class that extends JsonListener and fills in nine functions: startDocument(), startObject(), key(), value(), endObject(), startArray(), endArray(), endDocument() and whitespace(). The parser calls them in order. A sketch keeps track of the current key and picks out the one or two values it cares about, and the rest of the response is thrown away as it passes.

It does not report syntax errors. A broken document just produces fewer calls, so check that you got what you expected. The ThingPulse weather station projects are built on it.

Library Manager nameJson Streaming Parser
AuthorDaniel Eichhorn
Include#include <JsonStreamingParser.h>
#include <JsonListener.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <JsonStreamingParser.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Json Streaming Parser" by Daniel Eichhorn from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Json Streaming Parser to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.5, so a sketch written here builds there too.

What to know in the simulator: Pure software. As upstream, it reports no syntax errors (a malformed document gives fewer calls) and a \u escape above 0x7F becomes a space.

The calls you will use

In the order a sketch meets them.

JsonStreamingParser parser

The parser. It holds a 512 character buffer, so a single string longer than 511 characters is cut off.

parser.setListener(&listener)

Tells the parser which object to call. The listener must outlive the parsing.

parser.parse(c)

Feeds one character. Call it for every byte of the response, for example from a loop over client.read().

while (client.available()) {
  char c = client.read();
  parser.parse(c);
}

key(String key)

The listener function that hears each object key, without its quotes. Remember it, because the next value() belongs to it.

value(String value)

The listener function that hears every string, number, true, false and null as text. Convert it with toInt() or toFloat() when you need a number.

parser.reset()

Forgets a half-read document so the parser can start on a new one, for example after a connection drops.

Example: Listening to a JSON document one character at a time

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "JsonStreamingParser.h"
#include "JsonListener.h"

class ExampleListener : public JsonListener {
 public:
  void whitespace(char) {}
  void startDocument() { Serial.println("start document"); }
  void key(String key) { Serial.println("key: " + key); }
  void value(String value) { Serial.println("value: " + value); }
  void endArray() { Serial.println("end array"); }
  void endObject() { Serial.println("end object"); }
  void endDocument() { Serial.println("end document"); }
  void startArray() { Serial.println("start array"); }
  void startObject() { Serial.println("start object"); }
};

JsonStreamingParser parser;
ExampleListener listener;

void setup() {
  Serial.begin(9600);
  parser.setListener(&listener);
  const char *json = "{\"temp\": -3.5, \"city\": \"Oslo\\n\", \"hourly\": [1, true, null], \"w\": {\"gust\": 12e1}}";
  for (int i = 0; json[i] != 0; i++) {
    parser.parse(json[i]);
  }
}

void loop() {}

Open it in the editor, wired and ready to run

Common errors and how to fix them

The listener never hears anything

setListener() was not called, or the document does not start with a bracket. The parser waits for an opening { or [ before it starts, so strip any HTTP headers before feeding it.

A second document is ignored

After the outermost bracket closes the parser stops listening. Call parser.reset() before starting the next document, or the rest of the characters are dropped.

A value is the wrong one

Keys and values arrive as separate calls. Keep the last key in a variable, and for nested objects also keep the depth, because the same key name can appear at more than one level.

Try Json Streaming Parser without installing anything

Open the example, change the sketch, and keep your version in a free account.