Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get a buried variable from a JSON file?

This is probably a very novice question, but I am a very novice programmer, so here goes…

I am using GAS and Google Books to get the url for a book cover using this code:

function myFunction() {
  var url = "https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&country=US"
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var data = JSON.parse(json);
  Logger.log(response);
}

From my limited knowledge, I tried using this to get the proper URL:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Logger.log(data.items.imageLinks.smallThumbnail);

but it just returns an error. Is there something missing or a different way to get the variable I need?

>Solution :

The issue is that data.items is an array of 10 elements, therefore you have to index that.

If you want to access the first element:

function myFunction() {
  var url = "https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&country=US"
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var data = JSON.parse(json);
  console.log(data.items.length)
  Logger.log(data.items[0].volumeInfo.imageLinks.smallThumbnail);
}

If you want to access all elements and store it in an array:

function myFunction() {
  var url = "https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&country=US"
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var data = JSON.parse(json);
  var items = data.items;
  var smallThumbnails = items.map(x=>x.volumeInfo.imageLinks.smallThumbnail);
  Logger.log(smallThumbnails);
}

Output:

[http://books.google.com/books/content?id=gK98gXR8onwC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api, http://books.google.com/books/content?id=LRlCAAAAYAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api, http://books.google.com/books/content?id=Fgn65IL3q4wC&printsec=frontcover&img=1&zoom=5&source=gbs_api, http://books.google.com/books/content?id=3vFDvgAACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api, http://books.google.com/books/content?id=gK98gXR8onwC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api, http://books.google.com/books/content?id=F1wgqlNi8AMC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api, http://books.google.com/books/content?id=64tuPwAACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api, http://books.google.com/books/content?id=wAUiAAAAMAAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api, http://books.google.com/books/content?id=7hLQ_F0obXAC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api, http://books.google.com/books/content?id=tVxnDwAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading