Jonny
Jonny

Reputation: 16308

One liner to load json into variable

I'd like to load this JSON into a variable: http://itunes.apple.com/lookup?id=327630330

Is there a fast and quick method that does not require external libraries?

Upvotes: 0

Views: 119

Answers (2)

Jonny
Jonny

Reputation: 16308

My environment is Google Spreadsheets. I found out how to do it for this environment:

var result = UrlFetchApp.fetch("http://someurl/mydata.json");
var o  = Utilities.jsonParse(result.getContentText());

Reference here: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app

And FWIW I ended up writing this custom function to fetch title of an item (app, song etc) from iTunes by Apple ID. You can use it in Google Spreadsheets:

function jghgTitleForAppleid(appleid) {
  var theid = appleid;
  var completeurl = 'http://itunes.apple.com/lookup?&id=' + theid;

  response = UrlFetchApp.fetch(completeurl);
  var o = Utilities.jsonParse(response.getContentText());
  var title = o["results"][0]["trackName"]

  return title;
}

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816462

It looks like the service supports JSONP, so you just have to define a function and include a script tag pointing to the service:

<script>
function iTunesData(data) {
    // do something with data
}
</script>
<script src="http://itunes.apple.com/lookup?id=327630330&callback=iTunesData"></script>

Assuming of course you are running the code on the browser. You can also create the script element dynamically.

Upvotes: 3

Related Questions