There are several ways to serialize a JSON document; in this tutorial, we’ll only see the simplest.

Before writing any code, don’t forget to include the header:

The example

For our example, we’ll consider the following JSON document:

{"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}

The steps

1. Include ArduinoJson

Before writing any code, don’t forget to include the header:

#include <ArduinoJson.h>

2. Allocate a JsonBuffer

ArduinoJson uses a memory pool, the JsonBuffer, to perform all memory allocations.

In this tutorial, we create a 200-byte pool in the stack, with the following statement:

StaticJsonBuffer<200> jsonBuffer;

You can learn more on this topic in the page “ArduinoJson memory model”.

3. Create object tree in memory

We’ll now create an in-memory representation of the JSON document.

The document contains a JSON object as its root, so we start by creating a JsonObject. We do that by calling JsonBuffer::createObject()

JsonObject& root = jsonBuffer.createObject();

It’s important to understand that the JsonObject resides in the JsonBuffer and will be destructed with it.

Now we can add values to the root object:

root["sensor"] = "gps";
root["time"] = 1351824120;

To create the nested array “data”, we need to call JsonObject::createNestedArray():

JsonArray& data = root.createNestedArray("data");

To add the values in the array, we need to call JsonArray::add():

data.add(48.756080);
data.add(2.302038);

4. Serialize the document

Now that we have an in-memory representation of the JSON document, we can to convert it into a string; this step is called “serialization”.

To serialize a JsonObject, we just need to call JsonObject::printTo() and specify the destination.

For example, we can send the JSON document to the serial port:

root.printTo(Serial);

The statement above produces a minified JSON document.

To produce a prettified JSON document, we need to call JsonObject::prettyPrintTo():

root.prettyPrintTo(Serial);

Complete source code

// Step 1
#include <ArduinoJson.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) continue;

  // Step 2
  StaticJsonBuffer<200> jsonBuffer;

  // Step 3
  JsonObject& root = jsonBuffer.createObject();
  root["sensor"] = "gps";
  root["time"] = 1351824120;
  JsonArray& data = root.createNestedArray("data");
  data.add(48.756080);
  data.add(2.302038);

  // Step 4
  root.printTo(Serial);
  Serial.println();
  root.prettyPrintTo(Serial);
}

void loop() {}

See also

Keep learning

Mastering ArduinoJson

This tutorial was only a quickstart introduction to the library. For a complete course on ArduinoJson, I strongly recommend reading the book “Mastering ArduinoJson”.

Chapter 4 is a much longer tutorial on serialization. It starts with a simple example, like this one, but then gradually adds complexity to show all the features of the library. At the end of the chapter, we see how to upload measured data to Adafruit IO.

Chapter 2 is a quick C++ course to catch up with concepts like stack and heap memory, pointer, reference… I know from experience that new users of ArduinoJson have more difficulties with the language than with the library; that’s why the book starts with this course.

If you want to see what’s more in “Mastering ArduinoJson”, please check out the Table of Content.