arduino-libraries/Arduino_JSON

Enhancement: check if entry in JSON

step135 opened this issue · 2 comments

const char* sensor = doc["sensor"] | "unknown";

That could be ok, but when I use byte type and want to have all 256 numbers disponible, there is no solution.
I like that in Python I am able to do the check: using in. I need to implement following logic:

byte temp = 24; //default value
if("temp" in doc["temp"]) temp = doc["temp"]; //ovewriting the value if it is in JSON

OK, chatGPT adviced me to use doc.containsKey(), it works, but it is not documented under header "Extracting errors" at https://arduinojson.org/v7/tutorial/deserialization/

Hi @step135. This is the repository for the "Arduino_JSON" library. You are referring to the similarly named, but completely separate "ArduinoJson" library:

https://github.com/bblanchon/ArduinoJson

This library has a hasOwnProperty function that is equivalent to the "ArduinoJson" library's containsKey function, which is demonstrated in the example sketch:

// myObject.hasOwnProperty(key) checks if the object contains an entry for key
if (myObject.hasOwnProperty("result")) {
Serial.print("myObject[\"result\"] = ");
Serial.println((bool) myObject["result"]);
}
if (myObject.hasOwnProperty("count")) {
Serial.print("myObject[\"count\"] = ");
Serial.println((int) myObject["count"]);
}
if (myObject.hasOwnProperty("count")) {
Serial.print("myObject[\"count\"] = ");
Serial.println((double) myObject["count"]);
}
if (myObject.hasOwnProperty("foo")) {
Serial.print("myObject[\"foo\"] = ");
Serial.println((const char*) myObject["foo"]);
}

So the feature request is not applicable to this project.