ESP32 MicroPython: Encoding JSON

The objective of this post is to explain how to encode a JSON message using MicroPython and the ujson library on the ESP32.


Introduction

The objective of this post is to explain how to encode a JSON message using MicroPython and the ujson library on the ESP32. You can check how to enable the MicroPython support in this previous post. Also, if you need a guide on parsing JSON, check this previous tutorial.

 

The code

First of all, we need to import the ujson module, which is done with the following command:

import ujson

Important: at the time of writing, ujson was one of the modules included in the MicroPython firmware, so we can just import it without any additional procedure. Nevertheless, if you can’t import it out of the box, the module may no longer be included by default and need to be manually installed.

The example JSON message we will try to get is the one bellow. It is simulating a possible message from an IoT device, having a type of device (temperature sensor) and some dummy measurement values.

{
"deviceType": "Temperature",
"values": [23,22,25]
}

So, we need to put the structure of our message in a variable. We will use a Python dictionary, which works as a name-value structure. Thus, it suits well the structure of JSON.

We start by assigning an empty dictionary to a variable called dict. Then, we will assign the string “Temperature” to the key “deviceType”.

We will also assign a list with the values shown in the JSON to the key “values”. Note that a value for a Python dictionary can be an object [1], such as a list.

After the assignment, we will print the dictionary variable, just to confirm the values are correctly assigned.

dict = {}
dict["deviceType"] = "Temperature"
dict["values"] = [23,22,25]

print(dict)

So, after executing the previous code, we should get an output similar to figure 1. As can be seen, we have a dictionary structure with some name-value pairs. Note that the string representation of the Python dictionary is very similar of a JSON string.

ESP32 creating dictionary for JSON encoding

Figure 1 – Content of the Python dictionary.

To finalize, we will now convert the dictionary to the JSON string, by using the dumps function of the ujson module. This method receives as input the dictionary and returns its JSON string representation. After invoking this function, we will print the result.

encoded = ujson.dumps(dict)
print(encoded)

The final result can be seen bellow in figure 2, where the encoded string is printed. Note that the output is very similar to the string representation of the dictionary we got before, but the names and values are now between double quotes instead of just single ones, which is how JSON if formatted.

ESP32 MicroPython JSON encoding

Figure 2 – Final output of the JSON encoding program.


References

[1] http://www.compciv.org/guides/python/fundamentals/dictionaries-overview/

Advertisements
This entry was posted in ESP32 and tagged , , , , . Bookmark the permalink.

2 Responses to ESP32 MicroPython: Encoding JSON

  1. Pingback: ESP32 / ESP8266 MicroPython: HTTP POST Requests | techtutorialsx

  2. Pingback: ESP32 Picoweb: Serving JSON content | techtutorialsx

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s