Reputation: 616
I have a json from site https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo
It looks like
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2018-07-03",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-07-03": {
"1. open": "100.4800",
"2. high": "100.6300",
"3. low": "98.9400",
"4. close": "99.0500",
"5. volume": "14670275"
},
"2018-07-02": {
"1. open": "98.1000",
"2. high": "100.0600",
"3. low": "98.0000",
"4. close": "100.0100",
"5. volume": "19564521"
}
}
}
You can see in "Time Series (Daily)" there are different date objects. I want to access all the keys in each date and want to put data in array. I am using javascript and have no idea how to iterate through these different dates.
console.log(data['TimeSeries (Daily)']['2018-07-03']);
This is how i accessed date, but this is single date i want to iterate through each.
Upvotes: 2
Views: 992
Reputation: 4014
This will give you the output you want
var tSeries=data['Time Series (Daily)'];
for(var tempData in tSeries)
{
console.log(tSeries[tempData]);
console.log(tSeries[tempData]['1. open']);
console.log(tSeries[tempData]['2. high']);
}
Update
If you need to access dates and wants them in separate Array
var tempDataStore = [];
var tSeries=data['Time Series (Daily)'];
for(var tempData in tSeries)
{
// Here You get your Date
console.log("Your Date - " + tempData);
console.log(tSeries[tempData]);
console.log(tSeries[tempData]['1. open']);
console.log(tSeries[tempData]['2. high']);
tempDataStore.push({"Date":tempData,"open":tSeries[tempData]['1. open']})
}
console.log(tempDataStore)
Upvotes: 1
Reputation: 1098
Here is easiest way to do this...
const response = await fetch('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo');
const responseData = await response.json();
let allTimeStamps = [];
let allTimeStampsData = [];
for(let key in responseData["Time Series (Daily)"]){
allTimeStamps.push(key);
allTimeStampsData.push(responseData["Time Series (Daily)"][key]);
}
console.log("seperatedData", {allTimeStamps, allTimeStampsData});
try in the same domain to prevent cross-origin issue
Upvotes: 0
Reputation: 61
You could use a for in loop to get desired output
let myList = data['TimeSeries (Daily)'];
for (var index in myList)
{
console.log(myList[index]);
}
Regards
Upvotes: 0
Reputation: 10614
IMHO, this is what you are looking for:
data = {
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2018-07-03",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-07-03": {
"1. open": "100.4800",
"2. high": "100.6300",
"3. low": "98.9400",
"4. close": "99.0500",
"5. volume": "14670275"
},
"2018-07-02": {
"1. open": "98.1000",
"2. high": "100.0600",
"3. low": "98.0000",
"4. close": "100.0100",
"5. volume": "19564521"
}
}
}
Object.keys(data["Time Series (Daily)"]).forEach(date => console.log(date))
Upvotes: 0
Reputation: 29221
If you are using a modern JavaScript engine, or a transpiler like babel
this is cleanly done with Object.values
and Object.entries
:
Object.values(myData["Time Series (Daily)"]).forEach(function (date) {
Object.entries(date).forEach(function([period, value]) {
console.log(period, ':', value);
})
})
This would log the following in your console:
1. open : 100.4800
2. high : 100.6300
3. low : 98.9400
// ....
Upvotes: 1
Reputation: 1888
Loop through the keys of the "TimeSeries (Daily)" object. For example,
for(date in obj) {
console.log(obj[date]);
}
To access the data in each date, just add another loop.
Upvotes: 0