Decoding JSON file question

Hello,

I received the json file below from a Google API call. This is my first time dealing with a JSON file. So if I needed to get the name, address, rating and price level from each when available, how would I do this?  I looked at the docs for the JSON lib and still having trouble. Do I assign the text below to a variable and then decode it? Could someone show me some code on how to do this with the text below?

Thanks!!

{ "debug\_info" : [], "html\_attributions" : [], "next\_page\_token" : "CmRXAAAAt90EVao-f2Y6xfDi-FXkkZtKKLx-Lef6t0IPSrcssVmvkuoCIdxETCiCIvWHP0Ars6Q92K9NIeDIIKJMc1vgvVHdQZQfynfJz4ZkNtXgWn9hKz4PbJWhFlOWrSBX7GnnEhCCPaTOuKQww5CmVM7-pM6CGhSyY-yu-MHIRsx05PjHsCpp6ml1lg", "results" : [{ "geometry" : { "location" : { "lat" : 40.717923, "lng" : -74.00328500000001 } }, "icon" : "http://maps.gstatic.com/mapfiles/place\_api/icons/shopping-71.png", "id" : "9d48efdef3baaa003a959205563a4d07ca846b46", "name" : "Ricky's", "opening\_hours" : { "open\_now" : true }, "photos" : [ { "height" : 576, "html\_attributions" : [], "photo\_reference" : "CqQBnwAAADssUFDZ-7xWtRxZbx\_pa6CrY-Qgv1guMuZositS0k4KO7RZozTqoemNE0r2DSkuT9png4PpRMhJtWHP-fm-y\_1oVgBxt6daQX8oQU5gctvll1P8hnINaRX2d3aywMiSe9-lAcfmSQUofuobmIquWq3RJvFkehBn1Wq2T8HhCPpeWjofKzyyiqfQgdZhsfuM6ihZqGcOkWCVFgrEODnVJgISEK0yDtGjqaINiw-GggM-OpQaFCBBT7pwebJBmsF82WXRgj2d4oTs", "width" : 876 } ], "rating" : 3.2, "reference" : "CnRpAAAAfVwZ8-foC2u0JXoOVAgoxY0XGoTZ1NN0A1lDE0oDTO6FqhYMMkUqMmPb2lOUWqtss5T0rtdhVWteUJaqj4X5oAYx-GTUeUOGcEgRfQNdYPCloIsHwBop3rI0yKVuZkpBU8C4yOgjCTBGdFiMN3\_hvhIQU78OSZMnrVmSAOoZ4NJ-exoUYoQQ406rPHodf9mGARGJyghXNHw", "types" : ["clothing\_store", "store", "establishment"], "vicinity" : "375 Broadway, New York" }, { "geometry" : { "location" : { "lat" : 40.71797, "lng" : -74.005366 } }, "icon" : "http://maps.gstatic.com/mapfiles/place\_api/icons/shopping-71.png", "id" : "724e435b2ead533c239ed9317b8993e42a3f04bf", "name" : "O.K. Uniform Co., Inc.", "opening\_hours" : { "open\_now" : false }, "photos" : [{ "height" : 281, "html\_attributions" : [], "photo\_reference" : "CpQBgQAAACnIGi-EyGXX3iz4Ws4dLGqtkwpRjx2SZGjHroh\_IJAYFsMMc0sCkZ2mbwR406WQp8S-toV1u6narGxxY90DpUbIKnQ83QplKV8XnaWcRSPj6sN2R2EQ1W3-Tt7qDuL1-70s\_eYHQ49LxOmLnvHDe8dSHZerJPJOPviO30dRaAPBmQWpUf77r2Mpkh68tddznRIQQb993\_PvK37J8rxvYqbP3hoULBiBLPAFTuAtgObffHCk87gB\_iQ", "width" : 140 } ], "rating" : 3.7, "reference" : "CoQBeQAAAPq4UcGwKZPIO4myAsiCqSDZSmE\_UAi1DnIK8JgHS-FQrfJlCWYHofCU2Z07gq-lLXHPKOgjrROKfwIvg-JbxbuoJUNqqFPBxJB-e0Ur2RvNx6mPv8zENpLkP8aBarjrn7hU6M50ytAkEmFIukxPPJrEkYjFv\_OWWcyJt2Cg\_XafEhDOZAaDH1V1Yrx7pl4DJ\_1XGhQEgwS0VfPUP5Pzq8UnSPrHTOgp\_g", "types" : ["clothing\_store", "store", "establishment"], "vicinity" : "253 Church Street, New York" }, "status" : "OK" }

“Do I assign the text below to a variable and then decode it?”

Correct. I presume you’re receiving it in a network listener or something like that:

--make sure you require json somewhere in your project first local json = require("json") local function myGoogleAPIListener(event) --I'm guessing the event response syntax is "event.response.data" --you'll know the correct name for this local myJson = event.response.data --json decode converts the json string into a Lua table local myTable = json.decode(myJson) print(myTable.results.geometry.location.lat) --would print "40.717923" based on your example end --if you ever need to SEND json data somewhere, you can also do the opposite using json.encode() local myTable2 = { hello = "World", foo = "bar" } local myJson2 = json.encode(myTable2) print(myJson2) -- would print: {"hello":"World","foo":"bar"}

Thanks! Is there any way to loop through all of them in the file so I can list the names and other information from each?

When you say “loop through all of them”, do you mean loop through the “results” table within the main table?

If so, you can either use a numeric for loop:

local numberOfResults = #myTable.results --the #operator gives you the length of a table --The entries in your results table have no keys set manually --and so they will be numbered from 1 by default. --This means we can loop through them numerically for i = 1, numberOfResults do print(myTable.results[i].name) print(myTable.results[i].icon) --etc end

or you could use Lua’s “pairs” function:

for key, entry in pairs(myTable.results) do print(entry.name) print(entry.icon) --etc end

The pairs function is incredibly useful (especially when the keys in a table are not numeric), but it takes a little while to understand how to use it. I’d advise reading up on for loops and pairs when you have a chance.

The important thing to remember about all of this is that the result of json.decode will be a table, just like any other table you would use. It may have tables within tables, as in your json example, but if you play around with them for a bit you should get the hang of it (or by all means ask me another question if you need to).

“Do I assign the text below to a variable and then decode it?”

Correct. I presume you’re receiving it in a network listener or something like that:

--make sure you require json somewhere in your project first local json = require("json") local function myGoogleAPIListener(event) --I'm guessing the event response syntax is "event.response.data" --you'll know the correct name for this local myJson = event.response.data --json decode converts the json string into a Lua table local myTable = json.decode(myJson) print(myTable.results.geometry.location.lat) --would print "40.717923" based on your example end --if you ever need to SEND json data somewhere, you can also do the opposite using json.encode() local myTable2 = { hello = "World", foo = "bar" } local myJson2 = json.encode(myTable2) print(myJson2) -- would print: {"hello":"World","foo":"bar"}

Thanks! Is there any way to loop through all of them in the file so I can list the names and other information from each?

When you say “loop through all of them”, do you mean loop through the “results” table within the main table?

If so, you can either use a numeric for loop:

local numberOfResults = #myTable.results --the #operator gives you the length of a table --The entries in your results table have no keys set manually --and so they will be numbered from 1 by default. --This means we can loop through them numerically for i = 1, numberOfResults do print(myTable.results[i].name) print(myTable.results[i].icon) --etc end

or you could use Lua’s “pairs” function:

for key, entry in pairs(myTable.results) do print(entry.name) print(entry.icon) --etc end

The pairs function is incredibly useful (especially when the keys in a table are not numeric), but it takes a little while to understand how to use it. I’d advise reading up on for loops and pairs when you have a chance.

The important thing to remember about all of this is that the result of json.decode will be a table, just like any other table you would use. It may have tables within tables, as in your json example, but if you play around with them for a bit you should get the hang of it (or by all means ask me another question if you need to).