Encoding data in JSON is popular for sending data over the Internet, and also for storing data.
You can encode a Python List or dictionary in JSON format and then decode it back into a list or dictionary as illustrated in the diagram below:
Note: if you prefer video then I’ve create a YouTube video that covers this- How to Encode, Send and Receive JSON Data Using the Pythom MQTT Client
You will first need to import the json module
import json
To encode a python dictionary or list use json.dumps(data) as show below:
data_out=json.dumps(brokers_out) # encode object to JSON
To convert from a JSON string to a Python object use json.loads(json_string) as show below:
m_in=json.loads(m_decode) #decode json data
I’ve created a simple Python script that demonstrates the process.
The first part of the script encodes and decodes a Python Dictionary.
The second part of the script encodes a Python Dictionary Publishes the Data to the MQTT broker, then receives the data and decodes it back into a dictionary.
You can download the demo script using the link below
Here is a screen shot of what it looks like when run.
Notice the Python objects is converted to a string by the json.dumps() function.
Now we run the second part of the script which publishes the JSON data and then receives the Data and decodes it back into a dictionary.
Here is the on_message Callback which receives the messages and converts it to a dictionary.
def on_message(client, userdata, msg): topic=msg.topic m_decode=str(msg.payload.decode("utf-8","ignore")) print("data Received type",type(m_decode)) print("data Received",m_decode) print("Converting from Json to Object") m_in=json.loads(m_decode) #decode json data print(type(m_in)) print("broker 2 address = ",m_in["broker2"])
Using the Python Command Line
When working on decoding complex JSON strings I find it useful to paste the string into a Python command line and use it to decode the string.
The following screen shots illustrate this process.
Python List to JSON
Python Dictionary to JSON
JSON to Python
Below shows the JSON strings b and d being converted back to Python objects:
Extra White Space Problem
I was encoding an object like this:
msg2_on=json.dumps({"relay2":{"on":1}})
and the output looked ok but wasn’t working. On closer inspection and comparison with the output from the mosquitto_pub tool I noticed extra spaces had been added after each colon.
{"relay2": {"on": 0}}
The solution was to use the separator option.
msg2_on=json.dumps({"relay2":{"on":1}},separators=(',', ':'))
The output then looked like this:
{"relay2":{"on":0}}
See this for reference
Summary
JSON is a very popular format for encoding data and sending it across networks and also for storing it.
Python makes it easy to create JSON encoded data strings and to decode them.
Python Code
If you found this guide useful then perhaps you would like to Buy Me a Coffee
Related Tutorials :
- My Python Working Notes
- JSON Basics For Beginners-With Examples and Exercises
- Python Paho MQTT Client Send and Receive Integers and Floats
- Send JSON Data using the Mosquitto_pub client
- Send a File Using MQTT -MQTT Examples
- Encrypting MQTT Payloads with Python – Example Code
- Simple Python MQTT Data Logger
- Converting JSON to CSV with Python
Hi Steve, why do you use multiple broker ey? If I only use one broker is it okay the broker2 I put as broker1 also?
In my test scripts you will find multiple brokers configured just comment out the ones you aren’t using
Hi, this information was very helpful Steve, thank you! I was wondering whether there is a way of splitting large JSON data of an image in chunks, send them and then put them altogether again.
Hi
Not sure why it is JSON as the image is binary. Have you seen this tutorial
http://www.steves-internet-guide.com/send-file-mqtt/
rgds
Steve
In this we are seeing the JSON data is transmitted and received in the same code. How can we send and receive data using two computers running the JSON codes? Please help me this is my project. sending the JSON data from one computer to another computer which subscribed to the same topic using python paho MQTT.
Hi
just copy the script and on one comment out the publish and then run the subscribe first and then the publish.
We need to use same code for the two systems then.
Yes with minor edits
Thanks a lot, Steve
did you manage to find a solution for this ?
Thanks alot for these posts. As I’m new to python IOT world, I’ve been looking for a good place to learn the basics from & I think I’ve found it!
One issue though, I tried downloading the sample code file but I get an html page instead of a file. Could you maybe take a look at the kink again?
Thanks,
Link seems to work but I#ve sent the file by email
rgds
steve