Description

Gets, replaces or adds a value in the object.

Signatures

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

key: the key that the value will be associated to.

Return value

A reference to the JsonVariant in the object.

Implementation detail: in reality, a proxy class is returned instead of an actual JsonVariant to allow writing:

obj["key"] = "value"

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

JsonObject object = doc.to<JsonObject>();
object["hello"] = "world";
const char* world = object["hello"];

See also