Description

JsonObject::operator[] gets, replaces, or adds a value in the object pointed by the JsonObject.

If the JsonObject is null, this function does nothing.

If you need to know if the insertion succeed, use JsonVariant::set() instead.

Signatures

MemberProxy operator[](char* key) const;
MemberProxy operator[](const char* key) const;
MemberProxy operator[](const __FlashStringHelper* key) const;

MemberProxy operator[](const String& key) const;
MemberProxy operator[](const std::string& key) const;
MemberProxy operator[](std::string_view key) const;  // 🆕 (added in 6.18.1)

Arguments

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

As usual, ArduinoJson makes a copy of the key in the JsonDocument, except if it’s a const char*.

Return value

JsonObject::operator[] return a proxy class that allows using the JsonObject like a dictionary.

If this concept of proxy class is unfamiliar to you, just think of a JsonVariant instead.

String duplication

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

This duplication consumes some space in the JsonDocument; don’t forget to increase its capacity accordingly.

Example

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

See also