JSON and multi-dimensional arrays not working as expected

I have two identical tables (or I think they are). When I JSON encode and decode each, one has data and the other doesn’t. Please check out the code below to better understand my problem. Any ideas?

local map1 = { {0,1,0,1,0}, {0,1,0,1,0}, {0,1,1,1,0}, {0,0,0,0,0}, } local map2 = {} for y=0,4 do map2[y] = {} for x=0,6 do map2[y][x] = 0 end end local encodedMap1 = json.encode( map1 ) local decodedMap1 = json.decode( encodedMap1 ) print ("map 1 length after: " .. #decodedMap1) -- results in 4 local encodedMap2 = json.encode( map2 ) local decodedMap2 = json.decode( encodedMap2 ) print ("map 2 length after: " .. #decodedMap2) -- results in 0

Hi.

My guess would be that since your loops in the second case begin from 0, it’s getting treated as a hash table with string keys (“0”, “1”, etc.), and thus the length operator giving the result you see. Try for x = 1, 7 do and for y = 1, 5 do and see if you get what you want.

You’re right! I just tried it starting at 1 and the table remained intact after encoding/decoding. Thanks very much, this solves my problem.

Curious though because if its treating the keys as “0”, shouldn’t the following work? It doesn’t…

Anyway Im all set now thanks again for the fast answer!!!

local map2 = {} for y=0,4 do map2[tonumber(y)] = {} for x=0,6 do map2[tonumber(y)][tonumber(x)] = 0 end end

Glad to hear it.

On the other point, it would be the JSON library doing the conversions. Basically, it’s going to be following a policy like “if it looks like a Lua array, encode it as an array”, but the Lua convention for arrays is 1, 2, 3… Once you veer away from that, it falls back to the more general hash route, but unfortunately JSON objects only allow string keys. 

That makes sense. I’ll keep it in mind in the future. I’ll also start my table keys at 1. Thanks!

Hi.

My guess would be that since your loops in the second case begin from 0, it’s getting treated as a hash table with string keys (“0”, “1”, etc.), and thus the length operator giving the result you see. Try for x = 1, 7 do and for y = 1, 5 do and see if you get what you want.

You’re right! I just tried it starting at 1 and the table remained intact after encoding/decoding. Thanks very much, this solves my problem.

Curious though because if its treating the keys as “0”, shouldn’t the following work? It doesn’t…

Anyway Im all set now thanks again for the fast answer!!!

local map2 = {} for y=0,4 do map2[tonumber(y)] = {} for x=0,6 do map2[tonumber(y)][tonumber(x)] = 0 end end

Glad to hear it.

On the other point, it would be the JSON library doing the conversions. Basically, it’s going to be following a policy like “if it looks like a Lua array, encode it as an array”, but the Lua convention for arrays is 1, 2, 3… Once you veer away from that, it falls back to the more general hash route, but unfortunately JSON objects only allow string keys. 

That makes sense. I’ll keep it in mind in the future. I’ll also start my table keys at 1. Thanks!