ArduinoJson is an Arduino library that makes it easier to parse and generate JSON output. This is useful to save multiple parameters into a config file in SPIFFS or to generate or parse MQTT messages.

Usage[edit | edit source]

Install the ArduinoJson library in the Arduino library manager. Be aware that the latest version of ArduinoJson is version 6 and is not compatible with version 5.

AdrduinoJson 5[edit | edit source]

Include the header :

#include <ArduinoJson.h>

To generate JSON output, create a new JsonObject and use it like a dictionary. Use printTo() to generate the JSON output.

DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();

# You can specify values with various types. Eg. char* or literal strings
json["name"] = sensor_name;
json["command"] = "push";

# Floats and ints
json["measurement_f"] = 12.2;
json["measurement_i"] = 12;

# Boolean
json["measurement_b"] = true;

# Convert into a JSON message as char array
char payload[256];

To parse JSON data:

DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(jsonpayload);

// Copy out the strings or do parsing as necessary.
if (json.success()) {
	strcpy(mqtt_server, json["mqtt_server"]);
	mqtt_port = atoi(json["mqtt_port"])

See Also[edit | edit source]