Description

JsonObject::createNestedArray() adds a new array to the object pointed by the JsonObject.

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

Signatures

JsonArray createNestedArray(char* key) const;
JsonArray createNestedArray(const char* key) const;  // ⚠️ stores key as a pointer
JsonArray createNestedArray(const __FlashStringHelper* key) const;

JsonArray createNestedArray(const String& key) const;
JsonArray createNestedArray(const std::string& key) const;
JsonArray createNestedArray(std::string_view key) const;

Arguments

key: the key to associate with the array.

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

Return value

JsonObject::createNestedArray() returns a JsonArray that points to the newly created array.

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

Example

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

will print

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

See also