0

I would like to parse the gatecoin API JSON return.

They offer as an example return the following:

{
    "tickers": [
        {
            "currencyPair": "BTCUSD",
            "open": 200,
            "last": 200,
            "lastQ": 0.1,
            "high": 200,
            "low": 200,
            "volume": 50,
            "bid": 200,
            "bidQ": 27.7099,
            "ask": 203,
            "askQ": 10,
            "vwap": 0,
            "createDateTime": "1421252904"
        }
    ]
}

I would like to use BTCEUR as the currency pair, and I would like to then access the individual items. My code, in a Google sheet, is as follows:

function gatecoin()
{
  var response = UrlFetchApp.fetch("https://api.gatecoin.com/Public/LiveTickers");
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var json = JSON.parse(response.getContentText());
  var rate = json.tickers.currencyPair["BTCEUR"].last
  sheet.getRange(5, 2).setValue(rate);
}

I receive this error:

typeError: Cannot read property "BTCEUR" from undefined. (line 24, file "Code")

I have tried various other combinations like: tickers.currencyPair.BTCUSD.last and other variations with no success.

What is the correct way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1903663
  • 1,713
  • 2
  • 22
  • 44
  • I believe this is a duplicate of https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-or-property. The OP wants to find an object from the array that has "BTCEUR" as the value of the currencyPair key. – JJJ Sep 20 '17 at 16:12

1 Answers1

-4

tickers is an array. It has array elements, not a currencyPair property.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964