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 name | Json Streaming Parser |
|---|---|
| Author | Daniel Eichhorn |
| Include | #include <JsonStreamingParser.h>#include <JsonListener.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: 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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- ESP32-S3-CAM
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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() {}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.
Keep going
Try Json Streaming Parser without installing anything
Open the example, change the sketch, and keep your version in a free account.