Description

JsonObject::createNestedObject() appends a new object to the object pointed by the JsonObject.

If the JsonObject is null/unbound, this function does nothing.

Signature

JsonObject createNestedObject(char* key) const;
JsonObject createNestedObject(const char* key) const;  // ⚠️ stores key as a pointer
JsonObject createNestedObject(const __FlashStringHelper* key) const;

JsonObject createNestedObject(const String& key) const;
JsonObject createNestedObject(const std::string& key) const;
JsonObject createNestedObject(std::string_view key) const;

Arguments

key: the key to associate with the new object.

All types are stored by copy, except const char* which is stored by pointer.

Return value

JsonObject::createNestedObject() returns a JsonObject that points to the newly created object.

JsonObject::createNestedObject() returns null if the memory allocation fails; in which case JsonObject::isNull(), returns true.

Example

StaticJsonDocument<256> doc;
JsonObject root = doc.to<JsonObject>();
root["city"] = "Paris";
JsonObject weather = root.createNestedObject("weather");
weather["temp"] = 14.2;
weather["cond"] = "cloudy";
serializeJsonPretty(root, Serial);

will print

{
  "city": "Paris",
  "weather": {
    "temp": 14.20,
    "cond": "cloudy"
  }
}

See also

Global warming stripes by Professor Ed Hawkins (University of Reading)