1

I have been trying to use an API from https://api.nomics.com/ and I cant seem to use the data inside the JSON object. I want to get the "price" value and display it in the console log but it keeps saying my variable is undefined please assist:

app.post("/", function(req, res) {
request("https://api.nomics.com/v1/currencies/ticker?key=demo-26240835858194712a4f8cc0dc635c7a&ids=BTC&interval=1d&convert=USD&per-page=10&page=1", function(error, response, body) {

var data = JSON.parse(body);

var price = data.price;

console.log(data);

console.log(price);});});

The result I get is:

[ {
"id": "BTC",
"currency": "BTC",
"symbol": "BTC",
"name": "Bitcoin",
"logo_url": "https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/btc.svg",
"status": "active",
"price": "10609.61844458",
"price_date": "2020-10-07T00:00:00Z",
"price_timestamp": "2020-10-07T08:12:00Z",
"circulating_supply": "18510093",
"max_supply": "21000000",
"market_cap": "196385024104",
"num_exchanges": "361",
"num_pairs": "38888",
"first_candle": "2011-08-18T00:00:00Z",
"first_trade": "2011-08-18T00:00:00Z",
"first_order_book": "2017-01-06T00:00:00Z",
"rank": "1",
"rank_delta": "0",
"high": "19337.69352527",
"high_timestamp": "2017-12-16T00:00:00Z",
"1d": {
  "volume": "16456552525.84",
  "price_change": "-148.44596940",
  "price_change_pct": "-0.0138",
  "volume_change": "1509811120.10",
  "volume_change_pct": "0.1010",
  "market_cap_change": "-2739282102.40",
  "market_cap_change_pct": "-0.0138"
} }]

var price is undefined. is it because of the "[]" that is surrounding the JSON object?

1 Answer 1

2

Yes it's because the request you are making return an array of objects, so you need to get the first element and on that get the price. var price = data[0].price;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.