This library can be installed using the Arduino IDE library manager. After opening this tool, you just need to search for “ArduinoJson”, as shown in figure 1.
Figure 1 – Installing ArduinoJson from the Arduino IDE library manager.
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 first thing we will do is including the ArduinoJson library, so we can have access to the functions needed to serialize the JSON.
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.
Then we will declare an object of class StaticJsonDocument. It will hold the memory representation of our object .
Note that a StaticJsonDocument is a derived class from JsonDocument, with the particularity that memory is allocated on the stack . The alternative is the DynamicJsonDocument, where the memory is allocated on the heap . 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 .
Nonetheless, for larger documents, it is not recommended to use a StaticJsonDocument, to avoid stack overflow problems . 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 .
In our code, we need to specify the capacity of our StaticJsonDocument on a template parameter. The value is specified in bytes .
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
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.
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.
Note that we are simulating a JSON object with the following structure, representing a hypothetical sensor measurement:
Getting back to our Arduino code, to serialize the document to a JSON string, we will need a char buffer to store it.
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 . 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.
To finalize, we will print the content obtained in our char buffer. It should display a string containing our JSON document.
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. You should get an output similar to figure 2, which shows the JSON string getting printed.
Figure 2 – Output of the program, showing the JSON string.