How to Send and Receive JSON Data Over MQTT with Python

send-json-data-mqttEncoding 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:

send-json-data-diagram

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

download

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.

Json-encode-decode-pytho

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.

send-data-json-mqtt-example

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-list-json
Python Dictionary to JSON

Python-Dictionary-JSON

JSON to Python

Below shows the JSON strings b and d being converted back to Python objects:

json-python

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

download

Was This Useful?

Related Tutorials :

Please rate? And use Comments to let me know more

12 comments

  1. 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?

  2. 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.

  3. 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.

    1. Hi
      just copy the script and on one comment out the publish and then run the subscribe first and then the publish.

  4. 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,

Leave a Reply to steve Cancel reply

Your email address will not be published. Required fields are marked *