AlxMrx AlxMrx - 3 years ago 425
JSON Question

Fetch json API data from GDAX

I'm trying to get the BTC-EUR ticker from GDAX site to Google Spreadsheet using a script. I got this code but it doesn't work, always returning me error: The coordinates or dimensions of the range are invalid.

var baseUrl = 'https://api.gdax.com';
var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GDAX");

function ticker() {

var request = "/products/btc-eur/ticker";
var requestUrl = baseUrl + request;
var response = UrlFetchApp.fetch(requestUrl);
var json = JSON.parse(response.getContentText());

var rows = [],
jsondata;

for (i = 0; i < json.length; i++) {
jsondata = json[i];
rows.push([jsondata.price]);

}

dataRange = data.getRange(14, 1, rows.length, 1);
dataRange.setValues(rows);

}


I'm using the same code to push my balance to the spreadsheet, and it is working. I cannot understand why this one return me the error. If I log the json var I get the correct values from the site.
Anyone can help? Thank you

Answer Source

How about a following modification?

Modification points :

The JSON data from https://api.gdax.com/products/btc-eur/ticker is as follows. In this data, the value of price is only one. So when the JSON data is always like this, you can directly retrieve the value using json.price.

{
  "trade_id": 4314549,
  "price": "3691.06000000",
  "size": "0.00004053",
  "bid": "3691",
  "ask": "3691.05",
  "volume": "945.78845044",
  "time": "2017-01-01T00:00:00.000000Z"
}

When this is reflected to your script, the modified script is as follows.

Modified script :

function ticker() {
  var baseUrl = 'https://api.gdax.com';
  var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GDAX");
  var request = "/products/btc-eur/ticker";
  var requestUrl = baseUrl + request;
  var response = UrlFetchApp.fetch(requestUrl);
  var json = JSON.parse(response.getContentText());
  var rows = [[json.price]];
  var dataRange = data.getRange(14, 1, 1, 1);
  dataRange.setValue(rows);
}

If I misunderstand your question, I'm sorry.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download