How to get length of a json file?

I just noticed when loading a .json file I get length=0 instead the true length of the array.

Is this not working for .json and just for lua tables? How can I get the length for a json array when I have loaded it with: loadsave.loadTable ?

@d.mach: if I’m reading your question correctly, I suspect that the table that loadsave.loadTable returns probably has string (as opposed to integer) keys. Notoriously, Lua only “counts” integer keys when fetching the length of a table using the # operator (or in older versions of Lua, the table.getn method).

If you have non-integer keys, or mixed key types, your only choice that I’m aware of is to iterate through the table and count them yourself like this:

local count = 0 for k,v in pairs(some\_table) do count = count + 1 end print("The length of table some\_table is " .. count)

* sorry for the incomplete first submission of this post. I accidentally hit the wrong key and submitted it before I finished writing. Fixed in an edit.

1 Like

Thank you for your fast reply and explanation. I will try this asap.

Have a great Christmas! :wink: