#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
// Data parsing
DynamicJsonDocument doc(256);
String input = "{\"cmd\": \"ACK\", \"ts\": 1000000, \"data\": {\"origins\": [\"NIC\", \"COL\", \"HON\"], \"vendors\": [\"Royal Coffee\"]}}";
DeserializationError error = deserializeJson(doc, input);
if(error) {
Serial.print("Parse failed");
Serial.println(error.f_str());
abort();
}
serializeJsonPretty(doc, Serial);
JsonObject data = doc.as<JsonObject>();
// Get single value directly.
String cmd = data["cmd"];
Serial.println(cmd);
// Get all array values.
JsonArray origins = data["data"]["origins"].as<JsonArray>();
for (JsonVariant origin : origins) {
Serial.println(origin.as<String>());
}
// Get key value pairs.
for (JsonPair node : data) {
Serial.println(node.key().c_str());
serializeJsonPretty(node.value(), Serial);
Serial.println("");
// Serial.println(node.value().c_str());
}
// Check map containment.
Serial.println(data.containsKey("cmd"));
Serial.println(data.containsKey("code"));
// Data writing.
DynamicJsonDocument out(256);
JsonObject dataOut = out.as<JsonObject>();
dataOut["cmd"] = "createPallet";
dataOut["ts"] = millis();
dataOut["data"]["origin"] = "NIC";
dataOut["data"]["vendor"] = "Royal Coffee";
dataOut["data"]["received"] = "2022-08-16";
/*
// You can use a String as your JSON input.
// WARNING: the string in the input will be duplicated in the JsonDocument.
String input =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
deserializeJson(doc, input);
JsonObject obj = doc.as<JsonObject>();
// You can use a String to get an element of a JsonObject
// No duplication is done.
long time = obj[String("time")];
// You can use a String to set an element of a JsonObject
// WARNING: the content of the String will be duplicated in the JsonDocument.
obj[String("time")] = time;
// You can get a String from a JsonObject or JsonArray:
// No duplication is done, at least not in the JsonDocument.
String sensor = obj["sensor"];
// Unfortunately, the following doesn't work (issue #118):
// sensor = obj["sensor"]; // <- error "ambiguous overload for 'operator='"
// As a workaround, you need to replace by:
sensor = obj["sensor"].as<String>();
// You can set a String to a JsonObject or JsonArray:
// WARNING: the content of the String will be duplicated in the JsonDocument.
obj["sensor"] = sensor;
// It works with serialized() too:
obj["sensor"] = serialized(sensor);
// You can also concatenate strings
// WARNING: the content of the String will be duplicated in the JsonDocument.
obj[String("sen") + "sor"] = String("gp") + "s";
// You can compare the content of a JsonObject with a String
if (obj["sensor"] == sensor) {
// ...
}
// Lastly, you can print the resulting JSON to a String
String output;
serializeJson(doc, output);
*/
/*
doc["sensor"] = "gps";
doc["time"] = 1351824120;
// Add an array.
//
JsonArray data = doc.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);
// Generate the minified JSON and send it to the Serial port.
//
serializeJson(doc, Serial);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
// Start a new line
Serial.println();
// Generate the prettified JSON and send it to the Serial port.
//
serializeJsonPretty(doc, Serial);
// The above line prints:
// {
// "sensor": "gps",
// "time": 1351824120,
// "data": [
// 48.756080,
// 2.302038
// ]
// }
*/
}
void loop() {
// put your main code here, to run repeatedly:
}