Parsing JSON

I’m following the instructions on this page:
http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

Here’s the sample.json I’ve saved to my simulator:

{  
 "name": "Jack (\"Bee\") Nimble",   
 "format": {  
 "shape": "rect",   
 "width": 1920,   
 "height": 1080,   
 "interlace": false,   
 "framerate": 24  
 }  
}  

And I’ve got this going in my main.lua:

require "json"  
  
-- jsonFile() loads json file & returns contents as a string  
local jsonFile = function( filename, base )  
 if not base then base = system.ResourceDirectory; end -- set default base dir if none specified  
 local path = system.pathForFile( filename, base ) -- create a file path for corona i/o  
 local contents -- will hold contents of file  
 -- io.open opens a file at path. returns nil if no file found  
 local file = io.open( path, "r" )  
 if file then  
 -- read all contents of file into a string  
 contents = file:read( "\*a" )  
 io.close( file ) -- close the file after using it  
 end  
 return contents  
end  
  
local t = json.decode( jsonFile( "sample.json" ) )  
  
print(#t); --\> returns 0  
print(t.name); --\> Jack ("Bee") Nimble  
print(t.format.shape); --\> rect  

So this works, as described! Hurray!
But I’m trying to understand more complicated JSON files.
And Here’s an example of a JSON file I created and I’m attempting to parse:
(I do get the feeling I’m formatting it incorrectly)

{  
 { "firstname": "Jack", "lastname": "Black" },   
 { "firstname": "Jose", "lastname": "Ringo" },   
 { "firstname": "Janet", "lastname": "Macky" },   
 { "firstname": "Jones", "lastname": "Potter" }  
}  

I’m putting tables within the primary table, and I wish to parse it.

local myTable = json.decode( jsonFile( "sample.json" ) )  
for i=1,#myTable do  
 print( myTable[i][firstname] )  
end  

I’m getting a error pointing to the json.decode() section. I’m sure it’s a parsing issue, but I’m stuck. [import]uid: 154122 topic_id: 28176 reply_id: 328176[/import]

Try this.

local myTable = json.decode( jsonFile( "sample.json" ) )  
for i=1,#myTable do  
 print( myTable[i]["firstname"] ) -- Here's your error. Add " "   
end  

[import]uid: 13560 topic_id: 28176 reply_id: 113832[/import]

That worked!
Also, for some reason the json wasn’t being recognized as an asset in the Resources Directory.
I had to move it to the Documents Dir.

Here’s a question, concerning the more complication of the JSON itself…
I want to put a “status” inside the JSON, but before the names.
Something like this:

 {  
 "status": 1,   
 "clientCount": 4,  
 "clientrecords": [  
  
 { "firstname": "Jack", "lastname": "Black" },   
 { "firstname": "Jose", "lastname": "Ringo" },   
 { "firstname": "Janet", "lastname": "Macky" },   
 { "firstname": "Jones", "lastname": "Potter" }  
  
 ]  
 }  

To be honest, I’m sort of guessing here. You can tell I’m trying to fit as much info as I can into the string, with more variable possibilities, like pagination reasons, or server response statuses.

Does my syntax look correct? Am I putting the brackets in the right position?
And if so, do I loop through the “clientrecords” in a similar fashion?

[import]uid: 154122 topic_id: 28176 reply_id: 113833[/import]

Your json is valid, you can check if json is valid at JsonLint. I always check min there, there’s a bunch of places you can do that but I kinda got stuck at jsonLint and it does the job. Another app you can use is a free app called Jason. I even think theres a plugin for sublime text 2 that validates json, not sure though.

if you want to put the status before the names then you just do that in you r text string and add a little space between the words.

newText(group, jsonTable.status.." "..jsonTable.clientrecords["firstname"].." "..jsonTable.clientrecords["lastname"], x, y, width, height, font, size )  

another great library is the utility.lua, it prints tables real nice so you can look at them incase you get any errors. Think theres a modified version that is even better than the original. Search the forum or code exchange or even the samplecode.

[import]uid: 13560 topic_id: 28176 reply_id: 114095[/import]

You should loop through the clientrecords to get the count but I don’t know what the status is. Is it married, single, divorced etc then I would add that to;

{ “firstname”: “Jack”, “lastname”: “Black”, “status”:“single” },

[import]uid: 13560 topic_id: 28176 reply_id: 114096[/import]

I totally see the benefit to Jsonlint. I’ll use that from now on. Thanks!

The “status” is more of a server response variable. The return could be the number 1, meaning successful…, zero could mean access denied, or server un-responsive.
I’m honestly just playing with this portion, and I learning how to parse out the client table list from the rest.

I’m working on providing lists onto my device, and the lists could get rather numerous (client records are just a good way to practice for me)
So, I imagine I’ll have to learn how to properly Paginate, or load a limited amount of records - so not overload the memory.
I come from a web dev background, so I refer to Pagination. But I know that’s not how it works in the mobile world.
I’m checking out the utility.lua library today. [import]uid: 154122 topic_id: 28176 reply_id: 114098[/import]