Description

JsonObject::getMember() gets the value associated with the specified key. It returns a null reference if the key is not present in the object.

If the JsonObject is null, this function returns null.

Instead of this function, you can use JsonObject::operator[] which offers a more intuitive syntax.

Signatures

JsonVariant getMember(const char* key) const;
JsonVariant getMember(const __FlashStringHelper* key) const;

JsonVariant getMember(const String& key) const;
JsonVariant getMember(const std::string& key) const;

Arguments

key: the key of the value in the object.

Return value

JsonObject::getMember() returns a JsonVariant that points to the value.

JsonObject::getMember() returns null if it doesn’t find the key in the object; in which case JsonVariant::isNull() returns true.

Examples

The key is not found

Here is a program that shows what happens when the key is not found.

char json[] = "{\"username\":\"the_duke\"}";

StaticJsonDocument<256> doc;
deserializeJson(doc, json);

JsonObject object = doc.as<JsonObject>();

JsonVariant password = object.getMember("password");
if (password.isNull()) {
  Serial.println(F("Error: password is missing"));
}

This program prints:

Error: password is missing

The key is found

Here is a program that shows what happens when the key is found.

char json[] = "{\"username\":\"the_duke\"}";

StaticJsonDocument<256> doc;
deserializeJson(doc, json);

JsonObject object = doc.as<JsonObject>();

JsonVariant username = object.getMember("username");
Serial.println(F("Your name is: "));
Serial.println(username.as<const char*>());

This program prints:

Your name is: the_duke

See also