Casts JsonDocument to the specified type.

Unlike JsonDocument::to<T>(), this function doesn’t change the content of the JsonDocument.


JsonArray   as<JsonArray>();
JsonObject  as<JsonObject>();
JsonVariant as<JsonVariant>();

Return value

This function returns a reference to the root of the JsonDocument.

If the actual type of the root doesn’t match the requested type, this function returns a null reference. For example, suppose the JsonDocument is an array, if you call JsonDocument::as<JsonObject>(), it will return a null JsonObject.


DynamicJsonDocument doc(1024);
deserializeJson(doc, "{\"key\":\"value\")");

// get the JsonObject in the JsonDocument
JsonObject root =<JsonObject>();

// get the value in the JsonObject
const char* value = root["key"];

See also