Description

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

If the JsonObject is null, this function does nothing.

Signature

JsonObject createNestedObject(char* key) const;
JsonObject createNestedObject(const char* key) const;
JsonObject createNestedObject(const __FlashStringHelper* key) const;

JsonObject createNestedObject(const String& key) const; // see Remarks
JsonObject createNestedObject(const std::string& key) const; // see Remarks

Arguments

key: the key to associate with the new object.

Remarks

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.

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";
root.prettyPrintTo(Serial);

will print

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

See also