Parsing JSON

I am working on getting data from YouTube (API - channel statistics).

The server log ends-up with this JSON:

`2016-05-31 19:31:50 UTC-5 	[Agent]  {
 "kind": "youtube#channelListResponse",
 "etag": "\"mie-I9dfsd8dfs9810DLBkzLlg/NxRrjO_TcIdfjsdlif998roYYkQM\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 "items": [
   "kind": "youtube#channel",
   "etag": "\"I9dfsd8dfs9810DLBkzLlg/NxRrjO_TcIdfjsdlif998roYYkQM\"",
   "id": "UCwsdfksIkdj_19-cb4tpAfA",
   "statistics": {
    "viewCount": "6618",

I can use the JSONParser.class.nut to get the value of “kind”, but everything else, like “viewCount” is nested, so it won’t find them.

How can I parse the complex JSON array?
And why does it just say […truncated…] at the end?
Is there a character limit of the server log?

Why not use http.jsondecode() on the body of the response?

I tried that but ended-up with a table (I think?).
Most likely, I’m not understanding how to access the keys or tags in the table?

`local request=http.get(""+channel+"&key="+key,{ "Accept" : "application/json" } )
local responseTable=request.sendsync();
if (responseTable.statuscode == 200) 

local data=http.jsondecode(responseTable.body);

// not sure here how to read the various JSON elements of the array.
} else 
server.log("Error response: " + responseTable.statuscode);

2016-06-01 06:21:30 UTC-5 [Agent] (table : 0x7fc1c08ea420)

So the decode is an array (table)?
I don’t have a real good understanding of how the array elements are read.

Something like (haven’t tried it):

local viewCountToDisplay = data.item[0].viewCount.tointeger();

ie. table data has a key, ‘item’: get its first element, also a table, and return the value of the key ‘viewCount’. Since that’s rendered as a string, convert it to an integer.

@smittytone, thanks for the idea, but it didn’t work.

Key is actually ‘items’ not ‘item’, so I made that change …

local data=http.jsondecode(responseTable.body)
local viewCountToDisplay = data.items[0].viewCount.tointeger();

2016-06-01 15:50:46 UTC-5 [Agent] ERROR: the index ‘viewCount’ does not exist

With PHP, you can do a print_r and display a whole array (its values). Is there anything like that for table? A way to view its raw contents?

I wish there were some better script examples for Squirrel tables.

There’s a new prettyprinter library that helps with understanding data structures by printing them in readable format. I like it very much.

Viewcount appears to be in items[0].statistics you could try items[0].statistics.viewCount.tointeger()

If that fails then you could server.log the keys/values for each layer so you find exactly where it is.


YES! Using your idea of poking around with keys, I discovered the correct key path to get the number of subscribers. Thanks so much for pointing me in a direction.

This is what worked for me:

`local subscriberCountToDisplay = data.items[0].statistics.subscriberCount;

I used PHP to decode the JSON, then print_r to view how the array was constructed. That made the structure more apparent.

The array path I took is so close to what you described … it is nested under another array called ‘statistics’.

I’ll post the code on the other thread where I’m helping @GobiDoo recreate the YouTube Subscriber LED Display.

You posted at the same time as me. You got it though. Thanks.
I scratched my head for the longest time.