Parsing And Getting Data From Json
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. Inf
Solution 1:
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 Dateconsole.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)
Solution 2:
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
// ....
Solution 3:
Loop through the keys of the "TimeSeries (Daily)" object. For example,
for(datein obj) {
console.log(obj[date]);
}
To access the data in each date, just add another loop.
Solution 4:
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))
Solution 5:
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
Post a Comment for "Parsing And Getting Data From Json"