0

I'd like to consume the following URL in my Google Apps Script: http://a0.awsstatic.com/pricing/1/emr/pricing-mapr.min.js

Using the typical response = UrlFetchApp.fetch(url).getContentText() doesn't really work, it just returns the JSON surrounded by callback(). Is there a way to consume JSONP with Google Apps Script?

Code:

function myFunction() {
  getEmrPricingData_();

}

/*
 * Get EMR Prices
 *
 */
function getEmrPricingData_() {
  var emr_url = "http://a0.awsstatic.com/pricing/1/emr/pricing-mapr.min.js"
  var response = UrlFetchApp.fetch(emr_url).getContentText();
  Logger.log(response);
}
Chris Matta
  • 3,263
  • 3
  • 35
  • 48

1 Answers1

2

You need to define the function callback() in your code, and eval() your JSONP response to execute that function. For example to handle JSONP responses on a project of mine I created this function:

function callback(data){
    return data;
}

and in my request function:

var result = eval(UrlFetchApp.fetch(url + uri, options).getContentText());
Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
  • 1
    In case of security concerns would rather extract the data as a regex match rather than use eval. It depends on you view of how evil eval is. – JSDBroughton Jan 22 '15 at 20:18