LapdanceTransform
LapdanceTransform

Reputation: 1

Any ideas how to parse this payload in python?

I am using MQTT to subscribe to a topic and receive messages. I have this code written so far and it works fine subscribing and all but I am having problems determining a way to parse through the payload that I receive from the MQTT broker. I have been trying to obtain the numerical values that I see in the payload but I cannot figure out the right approach to parse this data. Any ideas/suggestions? Thanks!

My Code:

there is more code that is irrelevant. this is just a snippet.

import paho.mqtt.client as mqtt
import json

#Call back functions 

# gives connection message
def on_connect(client,userdata,rc):
print("Connected with result code:"+str(rc))
# subscribe for all devices of user
client.subscribe('+/devices/+/up')

def on_connect(client,userdata,rc):
print("Connected with result code:"+str(rc))
# subscribe for all devices of user
client.subscribe('+/devices/+/up')

def on_message(client,userdata,msg):
print"Topic",msg.topic + "\nMessage:" + str(msg.payload)

node_data = str(msg.payload)
print '\r\n test'
print node_data

j = json.loads(node_data)
print "\r\n after loads: "
print j

My Outputs:

Message:{"payload":"Dgv+CggGBCYFAPMLAgc=","fields":{"Light":58.32,"Pressure":98569.5,"Temp":32.4375,"X_accel":0.6875,"Y_accel":-0.125,"Z_accel":0.625},"port":1,"counter":8,"dev_eui":"000000007D9050C1","metadata":[{"frequency":904.3,"datarate":"SF7BW125","codingrate":"4/5","gateway_timestamp":2090979635,"gateway_time":"2016-07-28T02:26:15.386371Z","channel":2,"server_time":"2016-07-28T02:06:13.075194806Z","rssi":-13,"lsnr":9.5,"rfchain":0,"crc":1,"modulation":"LORA","gateway_eui":"0080000000009BE6","altitude":911,"longitude":-93.19677,"latitude":45.10303}]}


 test
{"payload":"Dgv+CggGBCYFAPMLAgc=","fields":{"Light":58.32,"Pressure":98569.5,"Temp":32.4375,"X_accel":0.6875,"Y_accel":-0.125,"Z_accel":0.625},"port":1,"counter":8,"dev_eui":"000000007D9050C1","metadata":[{"frequency":904.3,"datarate":"SF7BW125","codingrate":"4/5","gateway_timestamp":2090979635,"gateway_time":"2016-07-28T02:26:15.386371Z","channel":2,"server_time":"2016-07-28T02:06:13.075194806Z","rssi":-13,"lsnr":9.5,"rfchain":0,"crc":1,"modulation":"LORA","gateway_eui":"0080000000009BE6","altitude":911,"longitude":-93.19677,"latitude":45.10303}]}


 after loads: 
{u'fields': {u'Y_accel': -0.125, u'Temp': 32.4375, u'X_accel': 0.6875, u'Light': 58.32, u'Pressure': 98569.5, u'Z_accel': 0.625}, u'counter': 8, u'port': 1, u'dev_eui': u'000000007D9050C1', u'payload': u'Dgv+CggGBCYFAPMLAgc=', u'metadata': [{u'gateway_time': u'2016-07-28T02:26:15.386371Z', u'server_time': u'2016-07-28T02:06:13.075194806Z', u'datarate': u'SF7BW125', u'gateway_eui': u'0080000000009BE6', u'modulation': u'LORA', u'gateway_timestamp': 2090979635, u'longitude': -93.19677, u'crc': 1, u'frequency': 904.3, u'rfchain': 0, u'codingrate': u'4/5', u'lsnr': 9.5, u'latitude': 45.10303, u'rssi': -13, u'altitude': 911, u'channel': 2}]}

I want to be able to extract the numerical values from this...string?Dict?Json?(payload) for the 'Temp', 'Light', 'Pressure' etc...parameters.Any advice is greatly appreciated.' I have tried the following but I receive this error. Doesn't tell me much...

data = json.loads(node_data)
Press = data['Pressure']
print Press

File "C:\Python27\My_Py_27_Codes\scratch_py_mqtt.py", line 25, in on_message Press = data['Pressure']

KeyError: 'Pressure'

Apparently 'Pressure' is not a key in this Dictionary?

Upvotes: 0

Views: 8395

Answers (2)

LapdanceTransform
LapdanceTransform

Reputation: 1

So after a few hours of scratching my head and 'code-ing' away, I managed to achieve my goal. I am not sure if it is the 'prettiest' way or if it's even the best way to do it but here is what I figured out: I was dealing with a 'Dictionary' and I had to select the appropriate key-name 'fields'. This would then enable me to 'get' the numerical values associated with the parameters (Temp, Pressure, Light, etc...) that are inside the 'fields' portion of the Dictionary.

Code to solve my issue:

def on_message(client,userdata,msg):
print"Topic",msg.topic + "\n\nMessage:" + str(msg.payload)

node_data = str(msg.payload)

print ' \n Sensor Values \r'

my_dict = json.loads(node_data)
params = my_dict.get("fields",None)

light = params.get('Light')
pressure = params.get('Pressure')
temp = params.get('Temp')
x = params.get('X_accel')
y = params.get('Y_accel')
z = params.get('Z_accel')

print ''
print 'Light:' + str(light)
print 'Pressure:' + str(pressure)
print 'Temperature:' + str(temp)
print 'X-accel:' + str(x)
print 'Y-accel:' + str(y)
print 'Z-accel:' + str(z)
print '\n\n'

Cheers everyone!

Upvotes: 0

backtrack
backtrack

Reputation: 8154

node_data  = {"payload":"dsgsg","fields":{"pressure":34,"temp":35}}
data = json.loads(node_data)
Press = data['fields']['Pressure']
print Press

you have to give the exact location of the filed. In your case pressure is inside the fields dictionary

Upvotes: 3

Related Questions