ESP32 MicroPython: Parsing JSON

The objective of this post is to explain how to parse a JSON string with MicroPython running on the ESP32.


Introduction

The objective of this post is to explain how to parse a JSON string with MicroPython running on the ESP32. If you need help setting MicroPython on the ESP32, please check this previous post for a detailed guide. The guide also explains how to connect to the Python prompt.

In order to parse a JSON string, we will use the MicroPython uJSON library. You can also check the documentation of the library at Github.

Since we are going to use the command line for testing the code, we will need a tool to help us compress the JSON content in a single line, so we can easily paste it. So, we will use this website, which can receive a JSON string and compress it to a single line. Then, we can past it to Putty just by copying the JSON and right clicking on the terminal window.


The code

After connecting to the Python prompt, we are ready to start coding. So, the first thing we need to do is importing the uJSON module. Just type the expression bellow and hit enter.

import ujson

After that, we should have access to the ujson object, which makes available a method called loads. This method receives as input the JSON string and returns an object corresponding to the parsed JSON [1]. We will start with a simple JSON structure, which is the one shown bellow.

{
"name":"John"
},

After compressing it to a one liner, we get the JSON string shown bellow.

{"name":"John"},

So, we will now pass this string as input of the previously mentioned loads method. Note that we will enclose the string between “”” in each side, in order to escape the quotes from the JSON structure. We will store the object in a variable called parsed.

parsed = ujson.loads("""{"name":"John"}""")

Now, we will confirm that we have the content of the JSON correctly in our returning object. So we will print it. Additionally, we will print the type of the object with the type function.

print (parsed)
print (type(parsed))

After running the whole previous code, we should get an output similar to figure 1. Note that the type of our object with the parsed content is a Python dictionary, making it perfect for accessing the content in a key-value style.

ESP32 Micropython parsing JSON

Figure 1 – Parsing the JSON string.

Now, we will access the value for the key equal to “name”. It should return “John”. In order to access the value for that key in the dictionary, send the command bellow. Note that it is like accessing an array value but instead of using an index, we use a key, in the format of a string.

print(parsed["name"])

You should get an output similar to figure 2. Note that the name “John” is printed on the console.

ESP32 Accessing parsed JSON object.png

Figure 2 – Accessing the parsed values of the dictionary object.

To finalize our example, we will now parse a more complex structure, as shown bellow. This could represent, for example, a message sent from an IoT device.

{
"device":"temperature",
"id":543,
"values":[1,2,3]
}

After compressing, we get the following:

{"device":"temperature","id":543,"values":[1,2,3]}

So, we will now parse it and print all the keys existing in our JSON structure. We will also print the type of the “values” structure, to understand how it is mapped by the parser.

parsed = ujson.loads("""{"device":"temperature","id":543,"values":[1,2,3]}""")
print (parsed["device"])
print (parsed["id"])
print (parsed["values"])

print(type(parsed["values"]))

You should get a result similar to figure 3. As can be seen, all the values for each key are printed correctly. In the case of the “values” key, the structure inside the dictionary is a list, rather than a string representation of the values. Naturally, this is much better since we can operate over those values with all the functions available for lists, making them easier to manipulate.

ESP32 Parsing JSON with arrays.png

Figure 3 – Result of the JSON content parsed.


References

[1] https://docs.micropython.org/en/latest/pyboard/library/ujson.html

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

3 Responses to ESP32 MicroPython: Parsing JSON

  1. Pingback: ESP32 MicroPython: Encoding JSON | techtutorialsx

  2. Pingback: ESP32 / ESP8266 MicroPython: HTTP GET Requests | techtutorialsx

  3. Pingback: ESP32 / ESP8266 MicroPython: HTTP POST Requests | 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