Description

Creates a JsonArray as a child of the current object.

Signatures

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

Arguments

key: the key of the array 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 JsonArray. You can check JsonArray::isNull() to verify that the allocation succeeded.

Example

StaticJsonDocument<256> doc;
JsonObject root = doc.to<JsonObject>();
root["status"] = "on";
JsonArray levels = root.createNestedArray("levels");
levels.add(10);
levels.add(30);
root.prettyPrintTo(Serial);

will print

{
  "status": "on",
  "levels": [
    10,
    20
  ]
}

See also