Json parse - how to get value?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
tomes
Posts: 33
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

Json parse - how to get value?

Post by tomes »

Hi,
I have a JSON string:

Code: Select all

{"folder":{"testA":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1},"testB":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1},"testC":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1}}}
How can I get 2nd lever names using DzVents? I want to print testA testB testC
In my case these names may be different, so i must know all path to get for example item.json.folder.testA.tg
Or maybe there is a simpler way to extract these values?
I will be grateful for any hint.

Tom
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Json parse - how to get value?

Post by waaren »

tomes wrote: Sunday 05 April 2020 23:32 I have a JSON string:
How can I get 2nd lever names using DzVents?

In my case these names may be different, so i must know all path to get for example item.json.folder.testA.tg
Not sure I completely understand the question but this should get you going.

Code: Select all

return
{
    on =
    {
        devices =
        {
            'jsonTrigger' -- virtual switch to start the script.
        },
    },
       
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'json converter',
    },
   
    execute = function(dz)
        local lodash = dz.utils._ -- set of very useful tools
       
        myJSON = '{"folder":{"testA":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1},"testB":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1},"testC":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1}}}'
        myTable = dz.utils.fromJSON(myJSON)
       
        dz.utils.dumpTable(myTable) -- dumps the complete json (converted  to a table)
       
        for key, subTable in pairs(myTable.folder) do
            dz.log(key ..': ' .. lodash.str(subTable), dz.LOG_DEBUG) -- key are the tableNames below 'folder'
        end
       
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tomes
Posts: 33
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

Re: Json parse - how to get value?

Post by tomes »

Works perfectly! Thanks a lot.
So, i have another question. How to get KEY which have set isTalker:true?

Tom
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Json parse - how to get value?

Post by waaren »

tomes wrote: Monday 06 April 2020 15:48 Works perfectly! Thanks a lot.
So, i have another question. How to get KEY which have set isTalker:true?

Tom

Code: Select all

return
{
    on =
    {
        devices =
        {
            'jsonTrigger' -- virtual switch to start the script.
        },
    },
       
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'json converter',
    },
   
    execute = function(dz)
        local lodash = dz.utils._ -- set of very useful tools
       
        myJSON = '{"folder":{"testA":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1},"testB":{"isTalker":true,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1},"testC":{"isTalker":false,"monitoredTGs":[],"protoVer":{"majorVer":1,"minorVer":0},"tg":1}}}'
        myTable = dz.utils.fromJSON(myJSON)
        dz.utils.dumpTable(myTable) --

        for key, subTable in pairs(myTable.folder) do
            if subTable.isTalker then -- if true
                dz.log(lodash.str(key),dz.LOG_DEBUG)
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
tomes
Posts: 33
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

Re: Json parse - how to get value?

Post by tomes »

Great! Thanks again. Everything works perfectly!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest