Description

JsonObject::isNull() tests whether the JsonObject points to an object or not.

You can use this function to:

  1. check if the object was successfully parsed, or
  2. check if the object was successfully allocated.

As an alternative, you can use the conversion to bool; for example, if(object) instead of if(!object.isNull())

Signature

bool isNull() const;

Return value

JsonObject::isNull() returns a bool that tells if the JsonObject points to something:

Examples

Example 1: parsing success:

StaticJsonDocument<200> doc;
deserializeJson(doc, "{\"hello\":\"world\"}");
JsonObject object = doc.as<JsonObject>();
Serial.println(object.isNull()); // false

Example 2: parsing failure:

StaticJsonDocument<200> doc;
deserializeJson(doc, "[\"hello\",\"world\"]");
JsonObject object = doc.as<JsonObject>();
Serial.println(object.isNull()); // true

Example 3: allocation success:

StaticJsonDocument<200> doc;
JsonObject object = doc.to<JsonObject>();
Serial.println(object.isNull()); // false

Example 4: allocation failure:

StaticJsonDocument<1> doc;
JsonObject object = doc.to<JsonObject>();
Serial.println(object.isNull()); // true

See also

Global warming stripes by Professor Ed Hawkins (University of Reading)