1
var api_key_id = '1234567890'
function open(ticker, date) {
  ticker="TSLA";
  date="2019-01-14";
  var url = "https://api.polygon.io/v1/open-close/" + ticker + "/" + date + "?apiKey=" + api_key_id;
  var response = UrlFetchApp.fetch(url);
  var data = response.getContentText();
  Logger.log(data);
}

When the above code is ran it logs this:

{"status":"OK","from":"2019-01-14T03:30:00Z","symbol":"TSLA","open":342,"high":343.3,"low":335.37,"close":338.93,"afterHours":336.2148,"volume":4425458}

But when I try to do this:

Logger.log(data.open);

The output is:

undefined

Is the "data" variable not actually an object? What am I missing?

EDIT: Solution Found: Parsing the result of getContentText() as JSON allowed the desired variable to be called, see below. Thanks to chuckx for pointing this out!

var api_key_id = '1234567890'
function open(ticker, date) {
  var url = "https://api.polygon.io/v1/open-close/" + ticker + "/" + date + "?apiKey=" + api_key_id;
  var response = UrlFetchApp.fetch(url);
  var data = response.getContentText();
  var json = JSON.parse(data);
  Logger.log(json.open);
standard
  • 15
  • 1
  • 6
  • The request is asynchronous and the code was already executed by the time the response was returned due to which you are seeing it as `undefined`. – Sushanth -- Jan 15 '20 at 17:57
  • The [URL Fetch Service](https://developers.google.com/apps-script/reference/url-fetch) is not an asynchronous API. The issue here is that [HTTPResponse.getContextText()](https://developers.google.com/apps-script/reference/url-fetch/http-response.html#getContentText()) returns a string, so it is in fact not an object. – chuckx Jan 15 '20 at 18:36
  • chuckx if I try to log just the variable `response` from URLFetchApp it prints the exact same thing as logging the variable `data` after running getContextText(). I think you're right that it converts `response` into a string but I still get `undefined` when I call `response.open` meaning that the UrlFetchApp is also not returning an object or it is acting asynchronously. – standard Jan 15 '20 at 18:51
  • I did a type check on `response` and it is in fact returning an object after URLFetchApp. But `Logger.log(response.open)` is still `undefined`. – standard Jan 15 '20 at 19:12
  • `response` is an instance of the [`HTTPResponse` class](https://developers.google.com/apps-script/reference/url-fetch/http-response.html). Accessing the response content as properties of that class doesn't make sense. Alternatively, you can parse the JSON that the API returns, using something like `var api_response= JSON.parse(response.getContentText())`. – chuckx Jan 15 '20 at 19:24
  • 1
    Parsing the JSON allowed me to call the value I wanted! Thanks, this is solved. – standard Jan 15 '20 at 19:28
  • Could you post the solution, so others can see how you solved the issue? Thanks! – Jescanellas Jan 16 '20 at 08:39
  • Sure thing, I edited my post with the solution. – standard Jan 16 '20 at 15:19

1 Answers1

0

HTTPResponse.getContextText() returns a string, so it is in fact not an object.

Since this API returns JSON, you can parse the string using JSON.parse() and then interact with the returned object in the manner you're expecting.

...
var apiResponse= JSON.parse(response.getContentText());
Logger.log(apiResponse.open);
chuckx
  • 6,484
  • 1
  • 22
  • 23