I have a Arduino that publish a json string in MQTT server. I read the topic with python and see this:
{"temp1": 123, "vol1": 456, "cur1": 789, "pf1": 321, "p1":654}
How can I divide the various parts and assign them to a variable to be used later in the script?
es:
temp1 = 123
vol1 = 456
edit:
my json string is like this: {"temp1":26.44,"vol1":0.00,"cur1":0.04,"pf1":1.00,"p1":4000.00,"s1":0.00,"f1":48.84,"p1max":4000.00,"en1":16.00,"prev_en1":0.00,"m_en1":0.00,"prev_m_en1":0.00}
pacdata = msg.payload.decode("utf-8")
print("pacdata= ",str(pacdata))
my_set = str(pacdata)
print("my_set= ",my_set)
my_var = []
my_var1 = []
for x,y in my_set.items():
print (x,'=',y)
x = y
print("value temp1: ", my_set['temp1'])
If I write (copy) this string with your code all works fine, if I use the variable that contains this string the script don't work... Why?
Thanks!
You really do not need to divide a dictionary into various parts to keep each as variable to use later. A dictionary exist for the purpose usually, that keys can be used as your variables and the values as such whenever you need to. You only need to iterate through the dictionary this way to get want you want whenever:
As you'll notice, the keys can be declared as variables and the values assigned to them