ESP32 ArduinoJson: printing the keys of the JsonDocument

In this tutorial we will learn how to print all the keys of a JsonDocument, using the ESP32 and the Arduino core. This tutorial targets version 6 of the ArduinoJson library. The tests shown here were performed using an ESP32 board from DFRobot.

Introduction

In this tutorial we will learn how to print all the keys of a JsonDocument, using the ESP32 and the Arduino core. This tutorial targets version 6 of the ArduinoJson library.

This feature can be useful, for example, if we need to parse a JSON payload that has an unknown structure and we need to check which keys are available at run time.

Note however that, in this tutorial, we are setting the keys of the JsonDocument directly instead of obtaining them from a parsed payload. Nonetheless, the result achieved would be the same.

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

The code

As usual, the first thing we will do is importing the ArduinoJson.h library.

#include <ArduinoJson.h>

After this, we will move 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 create a StaticJsonDocument and add some data members to it. We are going to create a JSON like the one below, for testing purposes.

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

Since we are working with JSON, we need to specify both keys and values, as shown in the previous JSON structure. We set these on our StaticJsonDocument by using the subscript [] operator, as already covered here.

StaticJsonDocument<100> testDocument;

testDocument["sensorType"] = "temperature";
testDocument["sensorValue"] = 10;

Note however that, although we have started by creating a StaticJsonDocument, as soon as we add the first value, it will change its type to match either a JsonArray or a JsonObject [1].

In our case, since we are using the StaticJsonDocument as an object, it will become a JsonObject. In order to be able to iterate over the keys, we need to have a reference to the JsonObject.

Thus, to cast the StaticJsonDocument and obtain a reference to its root, we will use the as method [2].

Note that this method doesn’t change the content of the StaticJsonDocument and, if the actual type of the root doesn’t match the requested type, this function returns a null reference [2]. This means that, if our StaticJsonDocument was an array, it would return a null JsonObject [2].

JsonObject documentRoot = testDocument.as<JsonObject>();

After we have the JsonObject, we can iterate over its key value pairs using the range-based for loop syntax introduced in C++11 [3].

At each iteration, the iterator points to a JsonPair, which is a class that wraps a key and a value [3].

for (JsonPair keyValue : documentRoot) {
   // operate over the JsonPair
}

We can access the key by calling the key method on the JsonPair object. The key corresponds to a JsonString, which means that we need to call the c_str method to obtain the corresponding char pointer [3], so we are able to print it to the serial port.

Then we can simply print the result to the serial port.

for (JsonPair keyValue : documentRoot) {
    Serial.println(keyValue.key().c_str());
}

The final code can be seen below.

#include <ArduinoJson.h>
 
void setup() {
 
  Serial.begin(115200);
  
  StaticJsonDocument<100> testDocument;

  testDocument["sensorType"] = "temperature";
  testDocument["sensorValue"] = 10;
 
  JsonObject documentRoot = testDocument.as<JsonObject>();

  for (JsonPair keyValue : documentRoot) {
    Serial.println(keyValue.key().c_str());
  }
}
 
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 IDE serial monitor.

You should get an output similar to the one demonstrated in figure 1. As can be seen, both keys of our JSON where printed, as expected.

Output of the program, showing the JSON keys printed to the Arduino IDE serial monitor.
Figure 1 – Output of the program with the JSON keys,

References

[1] https://arduinojson.org/v6/api/jsondocument/

[2] https://arduinojson.org/v6/api/jsondocument/as/

[3] https://arduinojson.org/v6/api/jsonobject/begin_end/

2 thoughts on “ESP32 ArduinoJson: printing the keys of the JsonDocument”

  1. Tx Pal! I’m programming my ESP8266 to work with JSON, MQTT, Webserver, … and this together with your other articles helped me already a lot!
    KUTGW Mario

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading