How to check if value exists in ArduinoJSON

Option 1: Use containsKey()

arduinojson_containskey.cpp
if(doc.containsKey("speed")) {
    float value = doc["speed"];
} else {
    // Speed does not exist
}

Option 2: Use default values

example.cpp
// If doc["speed"] does not exist, speed will be NaN
float speed = doc["speed"] | nanf(nullptr);
if(speed != nanf(nullptr)) {
   // TODO do something with speed
}

 


Check out similar posts by category: Arduino, Embedded