JsonVariantConst is an immutable version of JsonVariant; only use it if you want to enforce immutability.


JsonVariantConst::as<T>() casts the value pointed by the JsonVariantConst to the specified type.


bool               as<bool>() const;

float              as<float>() const;
double             as<double>() const;

signed char        as<signed char>() const;
unsigned char      as<unsigned char>() const;
signed int         as<signed int>() const;
unsigned int       as<unsigned int>() const;
signed short       as<signed short>() const;
unsigned short     as<unsigned short>() const;
signed long        as<signed long>() const;
unsigned long      as<unsigned long>() const;
unsigned long long as<unsigned long long>() const;   // <- may require ARDUINOJSON_USE_LONG_LONG
signed long long   as<signed long long>() const;     // <- may require ARDUINOJSON_USE_LONG_LONG
signed __int64     as<signed __int64>() const;       // <- may require ARDUINOJSON_USE_INT64
unsigned __int64   as<unsigned __int64>() const;     // <- may require ARDUINOJSON_USE_INT64

const char*        as<char*>() const;
const char*        as<const char*>() const;
String             as<String>() const;
std::string        as<std::string>() const;

JsonArrayConst     as<JsonArray>() const;
JsonObjectConst    as<JsonObject>() const;

Return value

JsonVariantConst::as<T>() returns the value pointed by the JsonVariantConst casted to the specified type.

This function returns a default value if the cast is not possible. The default value is:

If you want to change the default value, you need to use JsonVariantConst::operator| instead.

Integer overflows

This function is aware of integer overflows and only returns a value if it can fit in the specified type.

For example, if the value contains 512, as<char>() returns 0, but as<int>() return 512.

This feature was added in ArduinoJson 6.10.0


StaticJsonDocument<256> doc;
deserializeJson(doc, "{\"answer\":42}");

JsonVariantConst answer = doc["answer"];
int i = answer.as<int>(); // <- i == 42
double d = answer.as<double>(); // <- d == 42.0
const char* s = answer.as<char*>(); // <- s == NULL

See also