JsonVariant::isNull() tells whether the variant points to a value or not.

Remember that a JsonVariant is a reference to a value in a JsonDocument. A JsonVariant that is not attached to a JsonDocument will always be null:

JsonVariant variant;
variant.set(42); // does nothing because the variant is not attached to a document
variant.isNull(); // returns true

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


bool isNull() const;

Return value

JsonVariant::isNull() returns a bool that tells if the JsonVariant points to something or not:


DynamicJsonDocument doc(1024);

doc["a"].isNull(); // false, because doc["a"] contains 1
doc["b"].isNull(); // true, because doc["b"] contains null
doc["c"].isNull(); // true, because doc["c"] doesn't exist

See also