Page 1 of 1

Json parse - how to get value?

Posted: Sunday 05 April 2020 23:32
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

Re: Json parse - how to get value?

Posted: Monday 06 April 2020 0:24
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
}

Re: Json parse - how to get value?

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

Tom

Re: Json parse - how to get value?

Posted: Monday 06 April 2020 16:42
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
}

Re: Json parse - how to get value?

Posted: Monday 06 April 2020 16:49
by tomes
Great! Thanks again. Everything works perfectly!