#include "ArduinoJson.h"
char sampleJSON[] = " {\
\"animal\": [\
{\
\"name\": \"cat\",\
\"age\": \"5\"\
},\
{\
\"name\": \"dog\",\
\"age\": \"3\"\
}\
],\
\"url\": \"http://192.168.1.1\"\
}";
StaticJsonDocument<128> doc;
void setup() {
Serial.begin(115200);
DeserializationError error = deserializeJson(doc, sampleJSON);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
} else {
Serial.println(F("BEFORE"));
serializeJsonPretty(doc, Serial);
// modify one entry that we know exists and we know its index
doc["animal"][0]["age"] = 10;
Serial.println(F("\n\nAFTER DIRECT MODIFICATION"));
serializeJsonPretty(doc, Serial);
// modify one entry that we know exists and we don't know its index
for (JsonObject animal : doc["animal"].as<JsonArray>()) {
const char* name = animal["name"];
if (strcmp(name, "cat") == 0) { // we found it
animal["age"] = 20;
break;
}
}
Serial.println(F("\n\nAFTER SEARCH AND MODIFICATION"));
serializeJsonPretty(doc, Serial);
}
}
void loop() {}