This example shows how to deserialize a JSON document with ArduinoJson.

An online demo of this example is available on

Source code

#include <ArduinoJson.h>

void setup() {
  // Initialize serial port
  while (!Serial) continue;

  // Allocate the JSON document
  // Inside the brackets, 200 is the size of the memory pool in bytes.
  // Don't forget to change this value to match your JSON document.
  // Use to compute the capacity.
  StaticJsonDocument<200> doc;

  // StaticJsonDocument<N> allocates memory on the stack, it can be
  // replaced by DynamicJsonObject which allocates in the heap.
  // DynamicJsonObject doc(200);

  // JSON input string.
  // It's better to use a char[] as shown here.
  // If you use a const char* or a String, ArduinoJson will
  // have to make a copy of the input in the JsonBuffer.
  char json[] =

  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, json);

  // Test if parsing succeeds.
  if (error) {
    Serial.print(F("deserializeJson() failed: "));

  // Get the root object in the document
  JsonObject root =<JsonObject>();

  // Fetch values.
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do root["time"].as<long>();
  const char* sensor = root["sensor"];
  long time = root["time"];
  double latitude = root["data"][0];
  double longitude = root["data"][1];

  // Print values.
  Serial.println(latitude, 6);
  Serial.println(longitude, 6);

void loop() {
  // not used in this example

Classes used in this example

Functions used in this example