[Resolved] Json table children

Hi there,

I’ve got a json-file:

  
{"response":  
{"url":{  
"2009-08-02":{"paid":1453,"organic":"11200"},  
"2009-08-09":{"paid":1177,"organic":"10938"},  
"2009-08-16":{"paid":1193,"organic":"11024"},  
"2009-08-23":{"paid":261,"organic":"11556"}  
}  
  

I want to get the children from url like that:

 local json = require ( "json" )  
 local str = functions.jsonFile( "visibility.json" )  
 local str = json.decode ( str )  
  
 print( str.response.url[1].paid )  
  

but it doesn’t work. is there a way to count the children in url and then get each one by it’s index?

by the way: calling the actual row by the string( in this case the date ) works:

 local json = require ( "json" )  
 local str = functions.jsonFile( "visibility.json" )  
 local str = json.decode ( str )  
  
 print ( str.response.url["2009-08-02"].paid )  
  

Thank you very much for your help!

[import]uid: 140000 topic_id: 32086 reply_id: 332086[/import]

Your table is constructed more like key-value pairs rather than an array with a numeric index. In Lua, tables provide both functionality.

When in key-value mode, there are no indexes so I can’t simply access them as 1, 2, 3, etc. The way you iterate over them is using the for loop with the pairs() function, something like:
for k,v in pairs(str.response.url) do
print(k, v)
end
[import]uid: 19626 topic_id: 32086 reply_id: 127836[/import]

hey robmiracle,

oh yes of course!

thank you so much, you’re my hero!

roman [import]uid: 140000 topic_id: 32086 reply_id: 127849[/import]

oh just one more question.

is it possible that it doesn’t go through the table step by step? cause my output seems to be unsorted now, it’s not in the right order:

output:

  
2012-09-16  
2012-10-07  
2012-09-23  
2012-09-30  
2012-10-14  
  

my current json-file:

{"response": {"url":{ "2012-09-16":{"paid":2785,"organic":"0"}, "2012-09-23":{"paid":2398,"organic":"10978"}, "2012-09-30":{"paid":2683,"organic":13060}, "2012-10-07":{"paid":3050,"organic":"11852"}, "2012-10-14":{"paid":1037,"organic":14803}}}, "response\_time":0.35, "count":4, "cached":"", "query\_key":"domain"} [import]uid: 140000 topic_id: 32086 reply_id: 127869[/import]

Your table is constructed more like key-value pairs rather than an array with a numeric index. In Lua, tables provide both functionality.

When in key-value mode, there are no indexes so I can’t simply access them as 1, 2, 3, etc. The way you iterate over them is using the for loop with the pairs() function, something like:
for k,v in pairs(str.response.url) do
print(k, v)
end
[import]uid: 19626 topic_id: 32086 reply_id: 127836[/import]

hey robmiracle,

oh yes of course!

thank you so much, you’re my hero!

roman [import]uid: 140000 topic_id: 32086 reply_id: 127849[/import]

oh just one more question.

is it possible that it doesn’t go through the table step by step? cause my output seems to be unsorted now, it’s not in the right order:

output:

  
2012-09-16  
2012-10-07  
2012-09-23  
2012-09-30  
2012-10-14  
  

my current json-file:

{"response": {"url":{ "2012-09-16":{"paid":2785,"organic":"0"}, "2012-09-23":{"paid":2398,"organic":"10978"}, "2012-09-30":{"paid":2683,"organic":13060}, "2012-10-07":{"paid":3050,"organic":"11852"}, "2012-10-14":{"paid":1037,"organic":14803}}}, "response\_time":0.35, "count":4, "cached":"", "query\_key":"domain"} [import]uid: 140000 topic_id: 32086 reply_id: 127869[/import]