Question regarding json table

I have the following table in json but it seems it can not be decoded to a usable lua table. What am I missing here? Is it the formatting?

{     {       [        {          "waveSpawnTime"    =   15000,          "spawnpoint":      [       1,      1,      1,      1,      1,      1],          "enemies":         [       4,      4,      4,      4,      4,      4],          "speed":           [     120,    120,    120,    120,    120,    120],          "spawntime":       [    1100,   1100,   1100,   1100,   1100,   1100],          "hitpoints":       [       2,      2,      2,      2,      2,      2],          "deathmoney":      [       5,      5,      5,      5,      5,      5],          "storePoints":     [      10,     10,     10,     10,     10,     10],          "rotationON":      [       1,      1,      1,      1,      1,      1],          "colorID":         [      14,     15,     16,     17,     18,     19],          "resistance":      [       0,      0,      0,      0,      0,      0],          "resstrength":     [       0,      0,      0,      0,      0,      0],          "HP":              [       0,      0,      0,      0,      0,      0],          "BarSpeed":        [       0,      0,      0,      0,      0,      0],         },         {          "waveSpawnTime"    =   15000,          "spawnpoint":      [       2,      2,      2,      2,      2,      2],          "enemies":         [       4,      4,      4,      4,      4,      4],          "speed":           [     120,    120,    120,    120,    120,    120],          "spawntime":       [    1100,   1100,   1100,   1100,   1100,   1100],          "hitpoints":       [       2,      2,      2,      2,      2,      2],          "deathmoney":      [       5,      5,      5,      5,      5,      5],          "storePoints":     [      10,     10,     10,     10,     10,     10],          "rotationON":      [       1,      1,      1,      1,      1,      1],          "colorID":         [      14,     15,     16,     17,     18,     19],          "resistance":      [       0,      0,      0,      0,      0,      0],          "resstrength":     [       0,      0,      0,      0,      0,      0],          "HP":              [       0,      0,      0,      0,      0,      0],          "BarSpeed":        [       0,      0,      0,      0,      0,      0],         },       ],   } }

How can I create json tables like the above and then use them as a lua table in corona to get the length of (for example) colorID and so on?

UPDATE: I get something like this as error: Decode failed at 1: value expected at line 1, column 1

You might want to look up json validators:  https://www.google.com/search?q=json+validator&oq=json+validator

They can point you to issues with your JSON data. Then our json.decode() should be able to turn that into a Lua table for you.

Rob

i like this one for decoding json tables

http://jsonviewer.stack.hu/

you could paste your code and try to minimize and edit it, until you figure out whats up.

kinda got curious

this works [{}]

but this doesnt [{{}}]

which should give a clue  :stuck_out_tongue:

You have “=” signs after waveSpawnTime entries. Also, with that table structure, you’d need to start and end with [], as in:

 

[{ "waveSpawnTime": 15000, "spawnpoint": [ 1, 1, 1, 1, 1, 1], "enemies": [4, 4, 4, 4, 4, 4], "speed": [120, 120, 120, 120, 120, 120], "spawntime": [1100, 1100, 1100, 1100, 1100, 1100], "hitpoints": [2, 2, 2, 2, 2, 2], "deathmoney": [5, 5, 5, 5, 5, 5], "storePoints": [10, 10, 10, 10, 10, 10], "rotationON": [1, 1, 1, 1, 1, 1], "colorID": [14, 15, 16, 17, 18, 19], "resistance": [0, 0, 0, 0, 0, 0], "resstrength": [0, 0, 0, 0, 0, 0], "HP": [0, 0, 0, 0, 0, 0], "BarSpeed": [0, 0, 0, 0, 0, 0], }, { "waveSpawnTime" : 15000, "spawnpoint": [2, 2, 2, 2, 2, 2], "enemies": [4, 4, 4, 4, 4, 4], "speed": [120, 120, 120, 120, 120, 120], "spawntime": [1100, 1100, 1100, 1100, 1100, 1100], "hitpoints": [2, 2, 2, 2, 2, 2], "deathmoney": [5, 5, 5, 5, 5, 5], "storePoints": [10, 10, 10, 10, 10, 10], "rotationON": [1, 1, 1, 1, 1, 1], "colorID": [14, 15, 16, 17, 18, 19], "resistance": [0, 0, 0, 0, 0, 0], "resstrength": [0, 0, 0, 0, 0, 0], "HP": [0, 0, 0, 0, 0, 0], "BarSpeed": [0, 0, 0, 0, 0, 0], }, ]

Oh, and for future reference, in JSON, you use square brackets [] for lists, i.e. arrays where you just have entries 1, 2, … n, etc. With curly brackets { } you need to provide key value pairs.

Thank you very much for all your fast help here! Much appreciated!

One more thing: I tried to decode many variations without success so far. I always get this:

Decode failed at 1: value expected at line 1, column 1

Any idea what this means?

You get the error because the first line is invalid. You are using a curly bracket but then you are not providing any key value pairs. Instead, you are providing just a list of other tables.

Also, you said that you tried many variations. Did you try the code that I provided?

Yes I did. Here: https://jsonformatter.org/

I then get

Parse error on line 19:
…    0,      0],    },    {        "waveSpa
----------------------^
Expecting ‘STRING’, got ‘}’

I have not enough nerves left for today to fix this :wink:

It’s that validator that you linked which is messed up. The code will run fine if you run it through json.decode in Corona.

My code in corona is:

local path = system.pathForFile( "testwaves.json", system.DocumentsDirectory ) local decoded, pos, msg = json.decode( path ) if decoded then         return decoded else         print( "Decode failed at "..tostring(pos)..": "..tostring(msg) )         return WaveInfo\_G1\_S2\_World1 end

And this is always resulting in the Decode failed with:

Decode failed at 1: value expected at line 1, column 1

i inserted the fixed code from Xedur into the online decoder i linked to before, and it worked fine.

He stated “You have “=” signs after waveSpawnTime entries”

Did you try to fix this? If you didnt, it would explain the error you are getting all the time.

it is after all value 1  :slight_smile:

Here’s a sample project that contains test.json file that contains the above mentioned data. The main.lua file contains a simple function for loading and decoding the file and afterwards the contents are printed out.

I now have reverse engineered the whole thing and finally got a json table like this:

[    [         {             "waveSpawnTime":15000,                          "spawnpoint":   [        1,      1,      1,      1,      1,      1,      1,      1],             "enemies":      [        4,      4,      4,      4,      4,      4,      4,      4],             "hitpoints":    [        2,      2,      2,      2,      2,      2,      2,      2],             "spawntime":    [     1050,   1050,   1050,   1050,   1050,   1050,   1050,   1050],             "speed":        [      120,    120,    120,    120,    120,    120,    120,    120],             "deathmoney":   [        5,      5,      5,      5,      5,      5,      5,      5],             "rotationON":   [        1,      1,      1,      1,      1,      1,      1,      1],             "scorePoints":  [       10,     10,     10,     10,     10,     10,     10,     10],             "colorID":      [       14,     15,     16,     17,     18,     19,     20,     21],             "resistance":   [        0,      0,      0,      0,      0,      0,      0,      0],             "resstrength":  [        0,      0,      0,      0,      0,      0,      0,      0],             "BarSpeed":     [        7,      7,      7,      7,      7,      7,      7,      7],             "HP":           [        2,      2,      2,      2,      2,      2,      2,      2]         },         {             "waveSpawnTime":15000,                          "spawnpoint":   [        2,      2,      2,      2,      2,      2,      2,      2],             "enemies":      [        4,      4,      4,      4,      4,      4,      4,      4],             "hitpoints":    [        2,      2,      2,      2,      2,      2,      2,      2],             "spawntime":    [     1050,   1050,   1050,   1050,   1050,   1050,   1050,   1050],             "speed":        [      120,    120,    120,    120,    120,    120,    120,    120],             "deathmoney":   [        5,      5,      5,      5,      5,      5,      5,      5],             "rotationON":   [        1,      1,      1,      1,      1,      1,      1,      1],             "scorePoints":  [       10,     10,     10,     10,     10,     10,     10,     10],             "colorID":      [       14,     15,     16,     17,     18,     19,     20,     21],             "resistance":   [        0,      0,      0,      0,      0,      0,      0,      0],             "resstrength":  [        0,      0,      0,      0,      0,      0,      0,      0],             "BarSpeed":     [        7,      7,      7,      7,      7,      7,      7,      7],             "HP":           [        2,      2,      2,      2,      2,      2,      2,      2]         },         {             "waveSpawnTime":15000,                          "spawnpoint":   [        3,      3,      3,      3,      3,      3,      3,      3],             "enemies":      [        4,      4,      4,      4,      4,      4,      4,      4],             "hitpoints":    [        2,      2,      2,      2,      2,      2,      2,      2],             "spawntime":    [     1050,   1050,   1050,   1050,   1050,   1050,   1050,   1050],             "speed":        [      120,    120,    120,    120,    120,    120,    120,    120],             "deathmoney":   [        5,      5,      5,      5,      5,      5,      5,      5],             "rotationON":   [        1,      1,      1,      1,      1,      1,      1,      1],             "scorePoints":  [       10,     10,     10,     10,     10,     10,     10,     10],             "colorID":      [       14,     15,     16,     17,     18,     19,     20,     21],             "resistance":   [        0,      0,      0,      0,      0,      0,      0,      0],             "resstrength":  [        0,      0,      0,      0,      0,      0,      0,      0],             "BarSpeed":     [        7,      7,      7,      7,      7,      7,      7,      7],             "HP":           [        2,      2,      2,      2,      2,      2,      2,      2]         }     ] ]

Still get the error with the above code BUT I now have changed the code for decoding to this:

                    local path = system.pathForFile( "testwaves.json", system.DocumentsDirectory )                     local file, errorString = io.open( path, "r" )                                       if not file then                         -- Error occurred; output the cause                         print( "File error: " .. errorString )                         return WaveInfo\_G1\_S2\_World1                     else                         -- Read data from file                         local contents = file:read( "\*a" )                         -- Decode JSON data into Lua table                         local t = json.decode( contents )                         -- Close the file handle                         io.close( file )                         -- Return table                         return t                     end

And now it finally is working! :slight_smile:

Thank you for all your help! I really appreciate your help and fast feedback!

Have a great weekend!

  • Daniela

you removed the “=” signs  :smiley:

And your original decoding script never opened the path for reading  :smiley:

netflix was getting dull anyway so nothing like a little bug hunting on friday evening.

i might even code a bit myself no then.

Have a great weekend as well.

I wondered what you are doing on a friday to get so fast answers! :slight_smile:

Thanks!

listening to some old but great freestyle music while trying some code tweaks, high on caffeine.

cheers!