$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 ArduinoJson v6 Tutorial: Serializing JSON

DFRobot Jun 11 2019 1430

In this esp32 tutorial we will check how to serialize JSON on the ESP32 using the ArduinoJson library. The tests shown here were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we will check how to serialize JSON on the ESP32 using the ArduinoJson library.

You can also use the library manager to update to the most recent version, which is version 6.

Note that, in previous tutorials, we have already covered some features of the ArduinoJson library, but for older versions. Here we are covering the most recent version, as already mentioned.

This version introduces some changes from the previous APIs. You can read here some information about migrating from version 5 to version 6.

The tests shown here were performed using an ESP32 board from DFRobot.

The code

The first thing we will do is including the ArduinoJson library, so we can have access to the functions needed to serialize the JSON.

#include <ArduinoJson.h>

Then we will move on to the Arduino setup function, where we will write the rest of our code. We will start by opening a serial connection, to output the results of our program.

Serial.begin(115200);

Then we will declare an object of class StaticJsonDocument. It will hold the memory representation of our object [1].

Note that a StaticJsonDocument is a derived class from JsonDocument, with the particularity that memory is allocated on the stack [2]. The alternative is the DynamicJsonDocument, where the memory is allocated on the heap [2]. For this tutorial, we will only make use of the StaticJsonDocument class.

One of the advantages of using the StaticJsonDocument is that it is slightly faster because it doesn’t rely on dynamic memory allocation [3].

Nonetheless, for larger documents, it is not recommended to use a StaticJsonDocument, to avoid stack overflow problems [3]. In the library documentation, it is recommended that we use StaticJsonDocument for documents smaller than 1 KB and DynamicJsonDocument for documents larger than 1 KB [4].

In our code, we need to specify the capacity of our StaticJsonDocument on a template parameter. The value is specified in bytes [2].

Naturally, the capacity we assign depends on the complexity of the JSON document. You can use this assistant to help you determine the size you will need for your use case. There are also some macros to help compute the sizeneeded, such as the 

JSON_OBJECT_SIZE.

Nonetheless, for our simple tutorial, we are going to use a capacity of 100 bytes, which is more than enough for the object we are going to represent.

StaticJsonDocument<100> testDocument;

Then, to add a new member to our document, we simply need to use the subscript operator []. You can check the syntax below for adding both members of our JSON.

testDocument["sensorType"] = "Temperature";
testDocument["value"] = 10;

Note that we are simulating a JSON object with the following structure, representing a hypothetical sensor measurement:

{
    "sensorType": "temperature",
    "value": 10
}

Getting back to our Arduino code, to serialize the document to a JSON string, we will need a char buffer to store it.

char buffer[100];

Then, to obtain the JSON string, we simply need to call the serializeJson function. As first input, we need to pass our StaticJsonDocument object and as second input the char buffer we previously declared.

Note that this function call will output a minified string, meaning that it won’t have spaces or line breaks [5]. Naturally, this occupies less space but it is much harder to read for a human. If you want to obtain a pretiffied string (adequate for a human to read), you should use the serializeJsonPretty function instead.

serializeJson(testDocument, buffer);

To finalize, we will print the content obtained in our char buffer. It should display a string containing our JSON document.

Serial.println(buffer);

The final code can be seen below.

#include <ArduinoJson.h>
 
void setup() {
 
  Serial.begin(115200);
   
  StaticJsonDocument<100> testDocument;
   
  testDocument["sensorType"] = "Temperature";
  testDocument["value"] = 10;
 
  char buffer[100];
 
  serializeJson(testDocument, buffer);
 
  Serial.println(buffer);
}
 
void loop() {
 
}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. When the procedure finishes, open the Arduino IDE serial monitor.


REVIEW