A variant can hold any type of values allowed by JSON:

  • a boolean
  • an integer
  • a floating point value
  • a string
  • an array
  • an object

A JsonVariant is a reference to this value, but a JsonDocument owns the data.

Because the JsonVariant is a just reference, you need a JsonDocument to create a JSON object. See the example below.


You’ll see that most member functions of JsonVariant are const. These methods do not modify the instance, but they may modify the value pointed by the JsonVariant.

As we said, a JsonVariant is a reference; the const-ness of the member functions refers to the reference object, not to the value.

ArduinoJson also supports a read-only reference type named JsonVariantConst. It’s similar to JsonVariant, except it doesn’t allow modifying the value.


Create a variant and serialize it

// allocate the memory for the document
DynamicJsonDocument doc(1024);

// create a variant
JsonVariant variant =<JsonVariant>();

// serialize the object and send the result to Serial
serializeJson(doc, Serial);

Deserialize a variant

// allocate the memory for the document
DynamicJsonDocument doc(1024);

// deserialize the object
char json[] = "42";
deserializeJson(doc, json);

// extract the data
JsonVariant variant =<JsonVariant>();
int value =<int>();

Member functions

JsonVariant also support the comparison operators ==, !=, <, <=, >, and >=.

See also