Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
Access most recent value in JSON nested object with JS

I'm trying to access the most recent element in the Time Series (5 min) object, without having to specify the date/time, after using this JS code:

var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);

var current_stock = JSON.parse(getStock.responseText);
console.log(current_stock);
var current_stock_price = current_stock["Time Series (5min)"][0]["4. close"];

So in this case (see screenshot) it's Time Series (5 min) > 2022-04-21 20:00:00 -> 4. close, but I get an undefined error.

I even tried in the developer console with the full JSON file. Using current_stock["Time Series (5 min)"] returns all of the child values in the console, but adding [0] or ["2022-04-21 20:00:00"] to the end throws an undefined error.

Raw JSON

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can access it like this:

var getStock = new XMLHttpRequest();
getStock.open("GET", "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);
var current_stock = JSON.parse(getStock.responseText);

const timeSeries = current_stock['Time Series (5min)']
const key = Object.keys(timeSeries)[0]
console.log(timeSeries[key]['4. close'])

about 4 years ago · Santiago Trujillo Relatório

0

When I change [0] to ["2022-04-21 20:00:00"], like you suggested it works just fine. The code will then be:

var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);

var current_stock = JSON.parse(getStock.responseText);
var current_stock_price = current_stock["Time Series (5min)"]["2022-04-21 20:00:00"]["4. close"];
console.log(current_stock_price)

If you don't want to use the keys you can use Object.values() to access the data:

var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);

var current_stock = JSON.parse(getStock.responseText);
var current_stock_price = Object.values(current_stock["Time Series (5min)"])[0]["4. close"];
console.log(current_stock_price)

This will return the latest entry.

about 4 years ago · Santiago Trujillo Relatório

0

That is because current_stock["Time Series (5min)"] is an object. Not an array, that's why the 0 index is not available. If you want to access the first item on current_stock["Time Series (5min)"] here's something you can do

var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);

var current_stock = JSON.parse(getStock.responseText);
console.log(current_stock);
var keys = Object.keys(current_stock["Time Series (5min)"]); // return all the keys in current_stock["Time Series (5min)"] object
console.log(keys) // keys is array, so you can access first item as keys[0]
var current_stock_price = current_stock["Time Series (5min)"][keys[0]]["4. close"]

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda