Json and lua table

I noticed that json’s encoding and decoding creates differences with lua tables

As in the example:

local test = { name = "test", other = "my other string", { kind = "Rect", --ecc, }, { kind = "Rect", --ecc, }, { kind = "Rect", --ecc, }, } print(#test) -- 3 for i=1, #test do --my code end local json = require("json") local encodeTable = json.encode( test ) local decodeTable = json.decode( encodeTable ) print(#decodeTable) -- 0 for i = 1, #decodeTable do --my code end

The second time I get that the decoded table length is equal to 0, can this be fixed?

Can you print out encodeTable?  We need more to go on.

Rob

You can’t encode and decode tables that have both numeric and non-numeric indexes.

If you start with this 

local tbl = { 10, 20, 30, name = "bob" }

then do this:

local etbl = json.encode( tbl ) local tbl2 = json.decode( etbl )

You will end up with this in tbl2 :

{ ["1"] = 10, ["2"] = 20, ["3"] = 30, name = "bob" }

In short, all numerically indexed entries are now string indexed.

SSK2 has a fix for this called repairIndicies()

If you have SSK2 installed, you can fix the table like this (only works for first level of table):

table.repairIndicies( tbl2 )

@Rob Miracle

Sure. Here it is:

{ "1":{ "kind":"Rect" }, "2":{ "kind":"Rect" }, "3":{ "kind":"Rect" }, "name":"test", "other":"my other string" }

@roaminggamer I think you hit the sign. I have tried with repairIndicies() and it actually works.

Right now the first level is fine but later I may need some other depth level…

I think at this point the only solution is to change somehow the structure of the basic table

Yes,  I suggest you not mix index types.

Alternately, you can download my code and modify it to iterate over the sub-tables.

Yes, I can modify your code but I prefer not to risk at this point anyway.

Thanks for the tips!

Can you print out encodeTable?  We need more to go on.

Rob

You can’t encode and decode tables that have both numeric and non-numeric indexes.

If you start with this 

local tbl = { 10, 20, 30, name = "bob" }

then do this:

local etbl = json.encode( tbl ) local tbl2 = json.decode( etbl )

You will end up with this in tbl2 :

{ ["1"] = 10, ["2"] = 20, ["3"] = 30, name = "bob" }

In short, all numerically indexed entries are now string indexed.

SSK2 has a fix for this called repairIndicies()

If you have SSK2 installed, you can fix the table like this (only works for first level of table):

table.repairIndicies( tbl2 )

@Rob Miracle

Sure. Here it is:

{ "1":{ "kind":"Rect" }, "2":{ "kind":"Rect" }, "3":{ "kind":"Rect" }, "name":"test", "other":"my other string" }

@roaminggamer I think you hit the sign. I have tried with repairIndicies() and it actually works.

Right now the first level is fine but later I may need some other depth level…

I think at this point the only solution is to change somehow the structure of the basic table

Yes,  I suggest you not mix index types.

Alternately, you can download my code and modify it to iterate over the sub-tables.

Yes, I can modify your code but I prefer not to risk at this point anyway.

Thanks for the tips!