How can I check the length of a table from JSON

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

Moderator: leecollings

Post Reply
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

How can I check the length of a table from JSON

Post by Delfuego »

I have a script running that checks if mobile devices are present on my WiFi network. For this I use the API of my Unifi controller. This API returns a string that I search for certain MAC adreses. Everything works fine.

Now I want to do more things with the data I receive from the controller. This is JSON and is converted into a LUA table by DzVents. I am able to read the data from the table:

Code: Select all

device.json.data[t].mac
By changing t from 1 to n in this example I am able to read all the mac adreses of devices that have a connection on my WiFi.
What I don't know is how many records there are (n). So I want to check the max of t; what is n.

Help is welcome!

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

Re: How can I check the length of a table from JSON

Post by waaren »

Delfuego wrote: Tuesday 03 November 2020 22:24 What I don't know is how many records there are (n). So I want to check the max of t; what is n.
#device.json.data gives you the number of entries in device.json.data if n is integer and 1..n is consecutive (no gaps)

the Lua way of looping over such a table is

Code: Select all

for index, value in ipairs(device.json.data) do
    print(index .. ': '.. tostring(value) )
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Delfuego
Posts: 32
Joined: Sunday 29 July 2018 16:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: How can I check the length of a table from JSON

Post by Delfuego »

Thank you! The simple #device.json.data did the job for me.

I also tried your LUA way. I'm not sure what to do with the result. Below you see first the result of the simple code (41) and next the LUA way.

I suppose that the LUA way can deal with missing numbers?!? Maybe you can elaborate on this a little bit more?

Code: Select all

2020-11-04 10:09:01.781 Status: dzVents: 41
2020-11-04 10:09:01.781 Status: dzVents: 1: table: 0xaebea348
2020-11-04 10:09:01.781 Status: dzVents: 2: table: 0xaebee998
2020-11-04 10:09:01.781 Status: dzVents: 3: table: 0xaebef520
2020-11-04 10:09:01.781 Status: dzVents: 4: table: 0xaebef4f8
2020-11-04 10:09:01.781 Status: dzVents: 5: table: 0xaebf1c60
2020-11-04 10:09:01.781 Status: dzVents: 6: table: 0xa44182f8
2020-11-04 10:09:01.781 Status: dzVents: 7: table: 0xa4419610
2020-11-04 10:09:01.781 Status: dzVents: 8: table: 0xa441a820
2020-11-04 10:09:01.781 Status: dzVents: 9: table: 0xa441be80
2020-11-04 10:09:01.781 Status: dzVents: 10: table: 0xa441d000
2020-11-04 10:09:01.781 Status: dzVents: 11: table: 0xa441e048
2020-11-04 10:09:01.781 Status: dzVents: 12: table: 0xa441eff8
2020-11-04 10:09:01.781 Status: dzVents: 13: table: 0xa44202d0
2020-11-04 10:09:01.781 Status: dzVents: 14: table: 0xa44216e0
2020-11-04 10:09:01.781 Status: dzVents: 15: table: 0xa4422a00
2020-11-04 10:09:01.781 Status: dzVents: 16: table: 0xa4423958
2020-11-04 10:09:01.781 Status: dzVents: 17: table: 0xa44249f0
2020-11-04 10:09:01.782 Status: dzVents: 18: table: 0xa4425d08
2020-11-04 10:09:01.782 Status: dzVents: 19: table: 0xa4427b60
2020-11-04 10:09:01.782 Status: dzVents: 20: table: 0xa5900398
2020-11-04 10:09:01.782 Status: dzVents: 21: table: 0xa5901388
2020-11-04 10:09:01.782 Status: dzVents: 22: table: 0xa59021f0
2020-11-04 10:09:01.782 Status: dzVents: 23: table: 0xa5902f58
2020-11-04 10:09:01.782 Status: dzVents: 24: table: 0xa5904100
2020-11-04 10:09:01.782 Status: dzVents: 25: table: 0xa59053a8
2020-11-04 10:09:01.782 Status: dzVents: 26: table: 0xa59064c0
2020-11-04 10:09:01.782 Status: dzVents: 27: table: 0xa5907530
2020-11-04 10:09:01.782 Status: dzVents: 28: table: 0xa59084d8
2020-11-04 10:09:01.782 Status: dzVents: 29: table: 0xa5909338
2020-11-04 10:09:01.782 Status: dzVents: 30: table: 0xa590a610
2020-11-04 10:09:01.782 Status: dzVents: 31: table: 0xa590b8b0
2020-11-04 10:09:01.782 Status: dzVents: 32: table: 0xa590cd88
2020-11-04 10:09:01.782 Status: dzVents: 33: table: 0xa590ddd8
2020-11-04 10:09:01.782 Status: dzVents: 34: table: 0xa590eda0
2020-11-04 10:09:01.782 Status: dzVents: 35: table: 0xa5910110
2020-11-04 10:09:01.782 Status: dzVents: 36: table: 0xa5911dc0
2020-11-04 10:09:01.782 Status: dzVents: 37: table: 0xa59ecf40
2020-11-04 10:09:01.782 Status: dzVents: 38: table: 0xa59edd10
2020-11-04 10:09:01.782 Status: dzVents: 39: table: 0xa59eeff0
2020-11-04 10:09:01.782 Status: dzVents: 40: table: 0xa59f02d0
2020-11-04 10:09:01.782 Status: dzVents: 41: table: 0xa59f1438
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How can I check the length of a table from JSON

Post by waaren »

Delfuego wrote: Wednesday 04 November 2020 10:15 Thank you! The simple #device.json.data did the job for me.
I also tried your LUA way. I'm not sure what to do with the result. Below you see first the result of the simple code (41) and next the LUA way.
I suppose that the LUA way can deal with missing numbers?!? Maybe you can elaborate on this a little bit more?

Code: Select all

-- Standard Lua method to walk a one dimensionable table with consecutive integer values from 1..n as keys (aka Array)
for index, value in ipairs(device.json.data) do
    print(index .. ': '.. tostring(value) )
end

Code: Select all

-- Standard Lua method to walk a one dimensionable table with other type keys (strings, non consecutive numbers, subtables, functions, etc..)
for key, value in pairs(device.json.data) do
    print(tostring(key) .. ': '.. tostring(value) )
end

Code: Select all

-- Funtion to print structure and content of any table
local function dumpTable(t, level)
    local level = level or "> "
    for attr, value in pairs(t or {}) do
        if (type(value) ~= 'function') then
            if (type(value) == 'table') then
                print(level .. attr .. ':')
                dumpTable(value, level .. '    ')
            else
                print(level .. attr .. ': ' .. tostring(value))
            end
        else
            print(level .. attr .. '()')
        end
    end
end

-- Call the function dumpTable
dumpTable(device.json)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest