Jump to content

JSON Help


paulm

Recommended Posts

Hi, first I'll show what kind of works, then what's not working at all. I'm using Google API app script, pretty much JavaScript, to insert text/stats from a YouTube url, inside a Google Doc. Running this code from script editor...

function createDoc() {  var doc = DocumentApp.getActiveDocument();  var body = doc.getBody();  var url = "a_long_url_with_auth_key";  var response = UrlFetchApp.fetch(url);  body.insertParagraph(4, response);    }

...inserts this data in new paragraph. Note viewCount key/pair...that's what I need to isolate (as View Count : #)

{"kind": "youtube#channelListResponse","etag": ""sGDdEsjSJ// some account stuff"pageInfo": { "totalResults": 1, "resultsPerPage": 1},"items": [ {  "kind": "youtube#channel",  "etag": ""sGDdEsjSJ_// account stuff  "id": "UCOa9jWlm8SIzlcERzZso0WA",  "statistics": {   "viewCount": "612280",   "commentCount": "0",   "subscriberCount": "1243",   "hiddenSubscriberCount": false,   "videoCount": "435"  } }]}

So I add JSON...

function createDoc() {  var doc = DocumentApp.getActiveDocument();  var body = doc.getBody();  var url = "this_all_works";  var response = UrlFetchApp.fetch(url);  var data = JSON.parse(response.getContentText());  body.insertParagraph(4, data);     }

which inserts only this in new paragraph (not all the key/value pairs above):

[object Object]

and appending viewCount:

body.insertParagraph(4, data.viewCount);

shows

undefined

So then I try a for loop, from which I can get no information to print to paragraph, nor even Logger.log (console). Logger.log does print logs from variables outside the loop. It's just not working inside the loop. So I'm not getting any feedback at all from this for loop.

function createDoc() {  var doc = DocumentApp.getActiveDocument();  var body = doc.getBody();  var url = "this_all_works";   var response = UrlFetchApp.fetch(url);  var data = JSON.parse(response.getContentText());    for (var i = 0; i < data.length; i++){      var obj = data[i];      Logger.log(obj); //also trying obj.viewCount v v       body.insertParagraph(4, obj);    } }

Seems to me something's breaking down at JSON level. Along with API docs, I've read the section on JSON. Thanks in advance for help.

 

Link to comment
Share on other sites

I don't think you understand JSON. The JSON code you showed isn't complete, it's missing another object on the top level. But, if that were there, and saved in your code as data, then that value is data.items[0].statistics.viewCount. Look at the structure of that JSON object.

Link to comment
Share on other sites

Made a lot of progress, thanks. I compared my data output to the docs, that it seems that my JSON code is complete?

 

I followed your clue about another object on top level and added nested parameters in url: fields=items(statistics(viewCount, subscriberCount)) which gave me what I needed. Then, I tried your data.items[0].statistics.viewCount defined in a variable, and that also works.

 

I'm seeing the nested structure better: [0] pertains to all keys in items because they're all in the first level of items.

Link to comment
Share on other sites

It does look like that JSON structure is fine, it's missing a level of indenting that threw me off.

I'm seeing the nested structure better: [0] pertains to all keys in items because they're all in the first level of items.

The 0 is an array index. data.items is an array, not an object. It's an array with 1 object in it. The square brackets define an array.
Link to comment
Share on other sites

If there were a closing } before statistics, then we'd get viewCount by data.items[1].statistics.viewCount right? Indexing with braces seems helpful to access name lists, etc. But because all these key/value pairs are unique (not list of names), why is group after semantics nested?

Link to comment
Share on other sites

Square brackets define arrays, and curly brackets define objects. The key/value pairs are properties of objects. The base object has the properties kind, etag, pageInfo, and items. The pageInfo property is an object with 2 other properties. The items property is an array with 1 element, which is an object. That object has properties called kind, etag, id, and statistics. The statistics property is an object with 5 other properties. There's a basic description of JSON here:http://json.orgJSON is shorthand and is valid syntax to use in Javascript. This will work fine to define the data object, it's valid code:

var data = {"kind": "youtube#channelListResponse","etag": ""sGDdEsjSJ// some account stuff"pageInfo": { "totalResults": 1, "resultsPerPage": 1},"items": [ {  "kind": "youtube#channel",  "etag": ""sGDdEsjSJ_// account stuff  "id": "UCOa9jWlm8SIzlcERzZso0WA",  "statistics": {   "viewCount": "612280",   "commentCount": "0",   "subscriberCount": "1243",   "hiddenSubscriberCount": false,   "videoCount": "435"  } }]};
You can do the same thing like this:
var data = new Object;data.kind = "youtube#channelListResponse";data.etag = ""sGDdEsjSJ";data.pageInfo = new Object;data.pageInfo.totalResults = 1;data.pageInfo.resultsPerPage = 1;data.items = new Array;var item = new Object;item.kind = "youtube#channel";item.etag = ""sGDdEsjSJ_";item.id = "UCOa9jWlm8SIzlcERzZso0WA";item.statistics = new Object;item.statistics.viewCount = "6122280";item.statistics.commentcount = "0";item.statistics.subscriberCount = "1234";item.statistics.hiddenSubscriberCount = false;item.statistics.videoCount = "435";data.items.push(item);
Obviously, using the shorthand JSON syntax is just a cleaner way to do all of that. It's the exact same thing though.
Link to comment
Share on other sites

Thanks, that makes the array-object model clear. One question though, how do the appended objects under your var = item know that they're coming from array "items": [ ? I'm not seeing recursion connecting them.

data.items = new Array;var item = new Object;
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...