Reading records of a JSON file - help

Hello,

I am working with reading in a JSON file and I want to loop through each of the records to read the data. Can someone show me the code for doing this? I have searched on here and can’t find the right code. Someone showed me this before but after an hour of searching…

Here is my example of a JSON file. Also, does it need the employees: there? 

{ "employees": [{ "name": "John", "Age": "10" }, { "name": "Joe", "Age": "11" }, { "name": "Jim", "Age": "12" }] }

Here is the other way without the title like it has “:employees” in the previous example. I can submit the data this way actually with just the array of data. I want to loop through each of these and read the color and value fields. Thanks!!!

[{ "color": "red", "value": "#f00" }, { "color": "green", "value": "#0f0" }, { "color": "blue", "value": "#00f" }]

Is there a reason you are not using json.decode() and having it turn it into a Lua table for you?

Hi Rob,

Actually I found the older post where you explained this to me there for reading multiple records. So after calling the URL and getting the data back I am doing it below. In my latest code I am looping through all of the records and inserting them into a SQLite table. I have written an asp.net page to retrieve the data from my MSSQL table and return them in JSON format using the newton king JSON control.

Warren

local function networkListener( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response local myTable = json.decode(myNewData) local numberOfResults = #myTable.Table1 print( numberOfResults ) print(myTable.Table1[2].Location) end end

Here is the other way without the title like it has “:employees” in the previous example. I can submit the data this way actually with just the array of data. I want to loop through each of these and read the color and value fields. Thanks!!!

[{ "color": "red", "value": "#f00" }, { "color": "green", "value": "#0f0" }, { "color": "blue", "value": "#00f" }]

Is there a reason you are not using json.decode() and having it turn it into a Lua table for you?

Hi Rob,

Actually I found the older post where you explained this to me there for reading multiple records. So after calling the URL and getting the data back I am doing it below. In my latest code I am looping through all of the records and inserting them into a SQLite table. I have written an asp.net page to retrieve the data from my MSSQL table and return them in JSON format using the newton king JSON control.

Warren

local function networkListener( event ) if ( event.isError ) then print( "Network error!") else myNewData = event.response local myTable = json.decode(myNewData) local numberOfResults = #myTable.Table1 print( numberOfResults ) print(myTable.Table1[2].Location) end end