The problem

You want to write {"p1":[0, 1]}, but instead you get {"p1":[]}.

The reason

When constructing the object, ArduinoJson tried to allocate memory in the JsonBuffer, but this allocation failed.

This failure can be due to:

  • the StaticJsonBuffer that is too small
  • the DynamicJsonBuffer that could not grow because there is not enough RAM left.

The solution

If you’re using a StaticJsonBuffer, try to increase its capacity. However, remember that a StaticJsonBuffer too big can cause a stack-overflow. Also, some platforms limit the size of the stack (e.g. 4KB on the ESP8266), so consider switching to a DynamicJsonBuffer that lives in the heap.

If you’re using a DynamicJsonBuffer, try to specify the capacity to its constructor; this will reduce the heap fragmentation.

In both case, use the ArduinoJson Assistant to compute the capacity of the JsonBuffer.

Still doesn’t work?

If none of this work, it probably means that your device doesn’t have enough RAM.

Try to reduce memory usage:

  • eliminate global variables
  • prefer const char* to String
  • reduce the size of each parameter (e.g. int -> byte)

Unfortunately, it is sometimes impossible to make your project run on a small microcontroller; in that case, your only option is to upgrade to a bigger one.

See also