Description

Creates a JsonObject as a child of the current object.

Signature

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

Arguments

key: the key of the object in the object, can be a const char* or a const String&

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 JsonBuffer to grow. The memory allocated for the copy will only be freed when the whole JsonBuffer is discarded.

Note that the rules changed in version 5.13.

Return value

A reference to the new JsonObject. You can check JsonObject::success() to verify that the allocation succeeded.

Example

StaticJsonBuffer<256> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
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

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