This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Json data to table problem
#1
I have a problem with json data from the MQTT broker.

I receive this string from the MQTT : {"Redox": "954","Ph": "6.67","Temperatuur": "10.5","Error message": ""}
When I check it (https://jsonformatter.curiousconcept.com/#) is valid JSON data.

When i decode this with 
payload = "{"Redox": "954","Ph": "6.67","Temperatuur": "10.5","Error message": ""}"
msg =  json.pdecode(payload)

The result is still a string instead of a table.

I guess i'm missing something obvious.

Can anyone help me?
Reply
#2
Are you sure you are logging the correct variable - msg instead of payload? If pdecode fails then the msg variable will be nil, not a string.
Reply
#3
Yes I’m sure

This is the code

mclient.ON_MESSAGE = function(mid, topic, payload)
local msg = json.pdecode(payload)
log ("Payload", type(payload)

log(type(msg),msg)
log(json.decode(msg).variable)
if type(msg) == 'table' then
Redox = msg.Redox
log("redox",Redox)
end
end
Reply
#4
It's possible that the payload is encoded twice so you get a string after first decode. Try decoding the msg again and post what you get.
Reply
#5
(28.03.2021, 12:23)admin Wrote: It's possible that the payload is encoded twice so you get a string after first decode. Try decoding the msg again and post what you get.

Indeed it was encoded twice.

Double decode solved it.

local msg = json.pdecode(json.pdecode(payload))

Thank you.
Reply


Forum Jump: