ESP8266: Encoding JSON messages

The objective of this post is to explain how to encode a JSON message using the ArduinoJson library.


The objective of this post is to explain how to encode a JSON message in the ESP8266, using the ArduinoJson library. We assume that the ESP8266 libraries for the Arduino IDE were previously installed. You can check how to do it here.

Also, for previous posts about decoding JSON messages, check the Related Posts section in the bottom.


First of all, we will include the library that implements the parsing functionality. The library can be obtained via library manager of the Arduino IDE.

#include <ArduinoJson.h>

For debugging purposes, we start a serial communication in the setup function.

void setup() {



Main loop

As we would do to decode a JSON message (check here the previous post), we also declare an object of class StaticJsonBuffer for encoding. We need to specify a size that is big enough for the structure we are going to create. In this case, we specified 300 bytes.

StaticJsonBuffer<300> JSONbuffer;

Then, we get a reference to a JsonObject from the StaticJsonBuffer object we created, by calling the createObject method.

JsonObject& JSONencoder = JSONbuffer.createObject();

In this case, we want to create a JSON message that will match the structure we used in a previous post, which has a sensor type and an array of values simulating measures from that sensor. The structure we want to create is shown below.

"SensorType" : "Temperature",
"Value" : [20,21,23]

To create simple name/value pairs in the JSON structure, we use the subscript operator or, in other words, we use square brackets.

JSONencoder[“sensorType”] = “Temperature”;

To create arrays, we call the createNestedArray method on the JsonObject reference.

JsonArray& values = JSONencoder.createNestedArray("values");

Then we use the add method of the JsonArray reference to add the values we want on our array.


Finally, we print the result to the serial connection, by using the printTo method of the JsonObject reference we defined early. We can directly pass the Serial connection to this method for the content do be directly printed. In this case, it will print the JSON message with the less overhead possible.


If we want to print the content indented, in a more user friendly format, we can call the prettyPrintTo method.


Naturally, we can also print the JSON message to a char buffer first, and only then print it to the serial console.

char JSONmessageBuffer[300];
JSONencoder.printTo( JSONmessageBuffer, sizeof(JSONmessageBuffer ) );

If we need to know the size of the JSON message we are going to print, we just call the measureLenght method, or the measurePrettyLength method, on the JsonObject reference. This will return the size of the string produced by the printTo and prettyPrintTo methods, respectively.

int lenghtPretty = JSONencoder.measurePrettyLength();
int lenghtSimple = JSONencoder.measureLength();

The final main loop function is specified bellow and includes some extra prints for easier debugging.

void loop() {

  StaticJsonBuffer<300> JSONbuffer;
  JsonObject& JSONencoder = JSONbuffer.createObject();

  JSONencoder["sensorType"] = "Temperature";
  JsonArray& values = JSONencoder.createNestedArray("values");


  int lenghtSimple = JSONencoder.measureLength();
  Serial.print("Less overhead JSON message size: ");

  int lenghtPretty = JSONencoder.measurePrettyLength();
  Serial.print("Pretty JSON message size: ");

  Serial.println("Less overhead JSON message: ");

  Serial.println("\nPretty JSON message: ");

  Serial.println("\nPretty JSON message from buffer: ");
  char JSONmessageBuffer[300];
  JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));



The expected output for the serial console is shown in figure 1.

ArduinoJson library JSON encoding

Figure 1 – Output of the JSON encoding program.

Related posts

Related content

Technical details

  • ESP8266 libraries: v2.3.0.
  • ArduinoJson library: v5.6.4.
This entry was posted in ESP8266, IoT, Microcontrollers and tagged , , , , , , , , , , , . Bookmark the permalink.

6 Responses to ESP8266: Encoding JSON messages

  1. Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx

  2. Pingback: ESP32: Parsing JSON | techtutorialsx

  3. Pingback: ESP32: Creating JSON message | techtutorialsx

  4. Pingback: ESP32: Sending JSON messages over MQTT | techtutorialsx

  5. Pingback: ESP8266: Adding Swagger UI to REST API | techtutorialsx

  6. Pingback: ESP32 Espruino: Deserializing JSON | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s