Json data to table problem - Printable Version +- Logic Machine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Json data to table problem (/showthread.php?tid=3262) |
Json data to table problem - bednarekluc - 28.03.2021 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? RE: Json data to table problem - admin - 28.03.2021 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. RE: Json data to table problem - bednarekluc - 28.03.2021 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 RE: Json data to table problem - admin - 28.03.2021 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. RE: Json data to table problem - bednarekluc - 28.03.2021 (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. |