Andy
Andy

Reputation: 1432

Accessing the first item in a JSON output using Javascript Fetch API

I'm trying to retrieve the latest entry from this JSON output. The data I am trying to retrieve is the latest entry, an example of which is below (the link above shows the entire json).

"Time Series (5min)": {
        "2018-11-21 16:00:00": {
            "1. open": "103.3300",
            "2. high": "103.4300",
            "3. low": "103.0800",
            "4. close": "103.0800",
            "5. volume": "1430557"
        },

Obviously I can specify the exact entry by referencing the

['2018-11-21 16:00:00']

using square bracket notation and similarly

['4. close']

in order to retrieve the data I need, but given that the latest entry is constantly switching date and time, the reference is always changing.

How do I ensure I am always retrieving the latest value?

Upvotes: 2

Views: 1838

Answers (4)

Tanmoy Krishna Das
Tanmoy Krishna Das

Reputation: 670

You can just convert the keys to Date and sort the keys in descending order. Then select the first one. Here's a snippet for that:

[This works even if the keys are not ordered when fetched initially]

var obj = {
  "Time Series (5min)": {
    "2018-11-21 16:00:00": {
      "1. open": "103.3300",
      "2. high": "103.4300",
      "3. low": "103.0800",
      "4. close": "103.0800",
      "5. volume": "1430557"
    },
    "2018-11-21 16:00:20": {
      "1. open": "104.3300",
      "2. high": "104.4300",
      "3. low": "104.0800",
      "4. close": "104.0800",
      "5. volume": "1430557"
    },
  }
}

var keys = Object.keys(obj['Time Series (5min)']);

//now sort the dates in descending order
keys.sort(function(a, b) {
    a = new Date(a);
    b = new Date(b);
    return a>b ? -1 : a<b ? 1 : 0;
});

var latestKey = keys[0];
var getVal = obj['Time Series (5min)'][latestKey]; //this is your latest data

console.log(getVal);

Upvotes: 2

Yousaf
Yousaf

Reputation: 29312

You can convert the Time Series (5min) object in to an array using Object.values method. Then access the first index of the array for latest entry

Here's how you get the latest entry using Fetch api

const url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo';

fetch(url)
   .then(response => response.json())
   .then(data => {
      const obj = data['Time Series (5min)'];
      const arr = Object.values(obj);
      const latestEntry = arr[0];
      console.log(latestEntry);
   })
   .catch(error => console.log(error));

Upvotes: 1

Freddy
Freddy

Reputation: 1229

You can't guarantee order in an object, so I don't think this is possible.

Upvotes: 0

brk
brk

Reputation: 50326

You can use Object.keys.It will give an array , then use the index, here 0 to get the first element

let obj = {
  "Time Series (5min)": {
    "2018-11-21 16:00:00": {
      "1. open": "103.3300",
      "2. high": "103.4300",
      "3. low": "103.0800",
      "4. close": "103.0800",
      "5. volume": "1430557"
    },
  }
}
let getKeys = Object.keys(obj['Time Series (5min)'])[0];
let getVal = obj['Time Series (5min)'][getKeys];
console.log(getVal)

Upvotes: 0

Related Questions