Description

Allows the JsonVariant to mimic a JsonArray or a JsonObject.

You can use this operator to chain the access to a nested member:

const char* weather = obj["weather"][0]["description"];

Signatures

// mimics a JsonArray
JsonVariant operator[](size_t index) const;

// mimics a JsonObject
JsonVariant operator[](const char* key) const;
JsonVariant operator[](char* key) const; // see Remarks
JsonVariant operator[](const String& key) const; // see Remarks
JsonVariant operator[](const std::string& key) const; // see Remarks
JsonVariant operator[](const __FlashStringHelper* key) const; // see Remarks

Arguments

index: the index in the JsonArray.

key: the key in the JsonObject

Return value

The value at specified index or key, or an undefined JsonVariant if the value doesn’t exist.

Remarks

ArduinoJson makes a copy of the string when you call this function with one of the following types:

  • char*
  • String (or std::string)
  • const __FlashStringHelper * (i.e. Flash string)

This duplication causes the JsonDocument to grow; so don’t forget to increase its capacity accordingly.

Example

JsonVariant variant;

variant = doc.parse("{\"hello\":\"world\"}");
const char* hello = variant["hello"];

variant = doc.parse("[\"hello\",\"world\"]");
const char* world = variant[1];

See also