var request = new XMLHttpRequest()
request.open('GET', 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo', true)
request.onload = function() {
var data = JSON.parse(this.response)
console.log(data)
}
request.send()
I have been working with the Alpha Vantage API to get stock data, but the way they format their data makes it almost impossible to access. I am using the demo data because I do not want to use my own API key because of the restrictions they put.
They put their object property names in strings, and there is usually more than one word in the string. This prevents me from accessing the property after I use
JSON.parse()
because using
JSON.parse()
causes the object property names to not be strings and just be property names, which disables my access to an individual property because object property names cannot be more than one word unless the names are strings.
Therefore, the API prevents me from accessing the object data. I use
JSON.parse()
because if I did not, the response I get from the API is a string itself, which I cannot use. I am trying to find an alternative to
JSON.parse()
that converts the string with all the object data inside of it to a regular object but the regular object still retains property names that are strings.
Here is what I am getting back after I use
JSON.parse()
:
{
Meta Data: {
1. Information:"Daily Prices (open, high, low, close) and Volumes",
2. Symbol:"MSFT",
3. Last Refreshed:"2019-09-24",
4. Output Size:"Compact",
5. Time Zone:"US/Eastern"
},
Time Series (Daily): {
2019-09-24: {
1. open:"140.3600",
2. high:"140.6900",
3. low:"136.8850",
4. close:"137.3800",
5. volume:"24602078"
},
2019-09-23: {
1. open:"139.2300",
2. high:"139.6300",
3. low:"138.4400",
4. close:"139.1400",
5. volume:"17139300"
},
2019-09-20: {
1. open:"141.0100",
2. high:"141.6500",
3. low:"138.2500",
4. close:"139.4400",
5. volume:"39167300"
},
2019-09-19: {
1. open:"140.3000",
2. high:"142.3700",
3. low:"140.0700",
4. close:"141.0700",
5. volume:"35772100"
}
}
}
If I do not use
JSON.parse()
, the result is this:
'{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2019-09-24",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2019-09-24": {
"1. open": "140.3600",
"2. high": "140.6900",
"3. low": "136.8850",
"4. close": "137.3800",
"5. volume": "24602078"
},
"2019-09-23": {
"1. open": "139.2300",
"2. high": "139.6300",
"3. low": "138.4400",
"4. close": "139.1400",
"5. volume": "17139300"
},
"2019-09-20": {
"1. open": "141.0100",
"2. high": "141.6500",
"3. low": "138.2500",
"4. close": "139.4400",
"5. volume": "39167300"
},
"2019-09-19": {
"1. open": "140.3000",
"2. high": "142.3700",
"3. low": "140.0700",
"4. close": "141.0700",
"5. volume": "35772100"
}
}
}
The object is wrapped in a string making it impossible to use.
What I have tried:
I have tried using JSON.parse() and that did not work.