Description

StaticJsonDocument is a JsonDocument that allocates its memory pool in-place, so it doesn’t rely on dynamic memory allocation.

Because it doesn’t call malloc() and free(), StaticJsonDocument is slightly faster than DynamicJsonDocument.

If you declare a local variable of type StaticJsonDocument, it allocates the memory pool in the stack memory. Beware not to allocate a memory pool too large in the stack because it would cause a stack overflow. Use StaticJsonDocument for small documents (below 1KB) and switch to a DynamicJsonDocument if it’s too large to fit in the stack memory.

Member functions

  • as<T>() casts the root to the specified type (e.g. JsonArray or JsonObject)
  • add() adds elements to the root array
  • capacity() returns the capacity of the memory pool
  • clear() empties the document and resets the memory pool
  • containsKey() tests if the root object contains the specified key
  • createNestedArray() creates a nested array attached to the root
  • createNestedObject() create a nested object attached to the root
  • getElement() is mostly for internal use 🤫
  • getMember() is mostly for internal use 🤫
  • getOrCreateMember() is mostly for internal use 🤫
  • operator[] gets or sets values in the document
  • is<T>() tests the type of the root
  • isNull() tells if the document is null or empty
  • memoryUsage() tells how many bytes are used in the memory pool
  • nesting() returns the number of nesting layers in the document
  • remove() removes an element (or member) at the specified index (or key)
  • set() is mostly for internal use 🤫

  • size() returns the number of elements (or members) that the root array (or object) contains
  • to<T>() clears the document and converts it to the specified type (e.g. JsonArray or JsonObject)

Example

Here is a program that deserializes a JSON document using a StaticJsonDocument

StaticJsonDocument<200> doc; // <- a little more than 200 bytes in the stack

char json[] = "{\"hello\":\"world\"}";
deserializeJson(doc, json);

const char* world = doc["hello"];

See also