Federico
Federico

Reputation: 1157

Parse Aws IoT message in python

I want to realize an AWS Lambda function that parse a json message received from IoT.

I receive this json:

{u'Ut': 1488467722, u'Rh': 59.4, u'Id': u'test', u'Temp': 21.6}

How can I parse this string to store each value into a variable?

Thanks

Upvotes: 0

Views: 300

Answers (1)

Tim B
Tim B

Reputation: 3133

You have a few options here, one thing that should work quite nicely is to make this a dictionary. You can do this using the inbuilt json module:

import json

orig_json_string = "{u'Ut': 1488467722, u'Rh': 59.4, u'Id': u'test', u'Temp': 21.6}"

json_string = orig_json_string.replace("u'", "\"").replace("'", "\"")
my_dict = json.loads(json_string)

print(my_dict['Ut'])
>>>> 1488467722

Note that the replace("u'", "\"") is only there because the question specifies a string with the unicode identifier included. This is only shown when you output a string so in general you should only use the replace("'", "\"") call.

Upvotes: 3

Related Questions