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 JsonDocument to grow; so don’t forget to increase its capacity accordingly.

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