Need help in decoding json Topic is solved

Moderator: leecollings

Post Reply
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Need help in decoding json

Post by ensingg »

I had a script to get the real values from the switches on my ESP8266. Unfortunately as of MEGA the json structure has changed with more deeper levels.
I cannot get it decoded.

I like to have the Value of the different Tasknumbers. Or if possible directly using the Taskname (Boven) and the the Value for Taskvalue with name "Switch".

Please help !

"TaskNumber":1,
"Type":"Switch input - Switch",
"TaskName":"Extra",
"TaskValues": [
{"ValueNumber":1,
"Name":"Switch",
"Value":0}]
},
{
"TaskNumber":2,
"Type":"Switch input - Switch",
"TaskName":"Boven",
"TaskValues": [
{"ValueNumber":1,
"Name":"Switch",
"Value":1}]
},
---

This is complete JSON:
{"System":{
"Build":20102,
"Git Build":"mega-20180504",
"System libraries":"ESP82xx Core 2_4_1, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.0.3",
"Plugins":72,
"Plugin description":" [Normal] [Testing] [Development]",
"Local time":"2018-05-07 09:23:51",
"Unit":0,
"Name":"esp_gdluik",
"Uptime":744,
"Last boot cause":"Manual reboot",
"Load":7,
"Load LC":11128,
"Free RAM":15656
},
"WiFi":{
"Hostname":"esp-gdluik-0",
"IP config":"Static",
"IP":"192.168.0.39",
"Subnet Mask":"255.255.255.0",
"Gateway IP":"192.168.0.1",
"MAC address":"A0:20:A6:25:70:F3",
"DNS 1":"8.8.8.8",
"DNS 2":"0.0.0.0",
"SSID":"Hanny24",
"BSSID":"AC:22:05:2E:80:9D",
"Channel":6,
"Connected msec":44602889,
"Last Disconnect Reason":203,
"Last Disconnect Reason str":"(203) Assoc fail",
"Number reconnects":0,
"RSSI":-79
},
"Sensors":[
{
"TaskNumber":1,
"Type":"Switch input - Switch",
"TaskName":"Extra",
"TaskValues": [
{"ValueNumber":1,
"Name":"Switch",
"Value":0}]
},
{
"TaskNumber":2,
"Type":"Switch input - Switch",
"TaskName":"Boven",
"TaskValues": [
{"ValueNumber":1,
"Name":"Switch",
"Value":1}]
},
{
"TaskNumber":3,
"Type":"Switch input - Switch",
"TaskName":"Beneden",
"TaskValues": [
{"ValueNumber":1,
"Name":"Switch",
"Value":0}]
},
{
"TaskNumber":4,
"Type":"Generic - Dummy Device",
"TaskName":"actie",
"TaskValues": [
{"ValueNumber":1,
"Name":"omhoog",
"Value":0.00},
{"ValueNumber":2,
"Name":"omlaag",
"Value":0.00},
{"ValueNumber":3,
"Name":"",
"Value":0.00},
{"ValueNumber":4,
"Name":"",
"Value":0.00}]
}
]
}
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Need help in decoding json

Post by jvdz »

This script assumes the data is stored in file testjson.txt:

Code: Select all

json = (loadfile "/usr/local/share/lua/5.2/JSON.lua") ()
file = io.open("testjson.txt", "r")
datainp=file:read("*a")
file:close()
input = json:decode(datainp)
result_table = input['Sensors']
tc = #result_table
for i = 1, tc do
   print(result_table[i].TaskNumber)
   print(result_table[i].TaskName)
   for count in pairs(result_table[i].TaskValues) do
      print('  '..count .. " ValueNumber:".. result_table[i].TaskValues[count].ValueNumber .. " Name:".. result_table[i].TaskValues[count].Name .. " Value:".. result_table[i].TaskValues[count].Value)
   end
end

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
gordonb3
Posts: 111
Joined: Saturday 31 March 2018 22:24
Target OS: Linux
Domoticz version: Custom
Location: Delft, NL
Contact:

Re: Need help in decoding json

Post by gordonb3 »

May like to note that Domoticz provides JSON.lua as well. It's in [domoticzhome]/scripts/lua
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest