Arduino_JSON: JSONVar values that work like JavaScript
Arduino_JSON is Arduino’s own JSON library, and it reads like JavaScript: one type, JSONVar, holds a number, a string, a boolean, an array or an object, JSON.parse() turns text into one, and JSON.stringify() turns one back into text. Random Nerd Tutorials’ ESP32 HTTP GET and web server examples use it, which is where most people meet it.
Built in on Mokxi: #include <Arduino_JSON.h> and it builds, with nothing to install. Free, and no account needed.
What Arduino_JSON does
Because a JSONVar can be anything, you check what you got with JSON.typeof(), which answers "undefined" when parsing failed, or "object", "array", "number" and so on. Values come out by assigning or casting: double lat = myObject["data"][0]; or (const char*) myObject["name"].
Printing a JSONVar prints it as JSON, so a string comes out with its quotes; cast it to const char* or String first when you want the bare text. Reading a key that is not there quietly adds it as null.
| Library Manager name | Arduino_JSON |
|---|---|
| Author | Arduino |
| Include | #include <Arduino_JSON.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 <Arduino_JSON.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Arduino_JSON" by Arduino from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Arduino_JSON to lib_deps in PlatformIO. Mokxi follows the calls of release 0.2.2, so a sketch written here builds there too.
What to know in the simulator: Numbers print with up to nine significant digits (seven on the AVR boards).
ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · 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 · 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.
JSONVar myObject = JSON.parse(jsonString)
Parses JSON text into a JSONVar. On bad input the result’s type is "undefined".
JSON.typeof(myObject)
The value’s type as a String: "undefined", "null", "boolean", "number", "string", "array" or "object".
JSONVar data = JSON.parse(payload);
if (JSON.typeof(data) == "undefined") {
Serial.println("Parsing input failed!");
return;
}myObject.hasOwnProperty("temp")
True when the object has that key, checked before reading so a missing key is not added as null.
JSONVar keys = myObject.keys()
An array of the object’s keys, for walking every member with a for loop.
keys.length()
The number of items in an array, or characters in a string. For an object it is -1; use keys().length() instead.
myObject["led"] = true
Adds or changes a member, with any type of value.
JSON.stringify(myObject)
The value as compact JSON text, a String you can send or print.
Example: Parse, read, change and print JSON
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Arduino_JSON.h>
const char input[] = "{\"sensor\":\"gps\",\"time\":1351,\"data\":[48.75,2.5]}";
void setup() {
Serial.begin(9600);
JSONVar myObject = JSON.parse(input);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON.typeof(myObject) = ");
Serial.println(JSON.typeof(myObject));
if (myObject.hasOwnProperty("sensor")) {
Serial.print("myObject[\"sensor\"] = ");
Serial.println((const char *)myObject["sensor"]);
}
double latitude = myObject["data"][0];
Serial.print("latitude = ");
Serial.println(latitude);
JSONVar keys = myObject.keys();
Serial.print("keys = ");
Serial.println(keys);
myObject["led"] = true;
myObject["data"][2] = 7;
Serial.print("now ");
Serial.println(myObject);
Serial.print("bad input is ");
Serial.println(JSON.typeof(JSON.parse("{oops")));
}
void loop() {}Common errors and how to fix them
Serial prints "gps" with the quotes
Printing a JSONVar prints its JSON, and a JSON string has quotes. Cast it first: Serial.println((const char*) myObject["sensor"]); or assign it to a String.
A number reads as 0, or the comparison is never true
Cast the JSONVar to the type you want before using it: int count = (int) obj["count"]; Comparing a JSONVar straight to a number or a string literal does not always pick the conversion you expect.
conflicting declaration or 'typeof' errors when another library is included
Arduino_JSON defines typeof and null as macros, which can clash with code that uses those names. Include it last, and do not use it in the same sketch as ArduinoJson.
Keep going
Try Arduino_JSON without installing anything
Open the example, change the sketch, and keep your version in a free account.