Click here to Skip to main content
15,891,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var request = new XMLHttpRequest()

request.open('GET', 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo', true)
request.onload = function() {
  // Begin accessing JSON data here
  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.
Posted
Updated 25-Sep-19 9:01am
v4
Comments
MadMyche 23-Sep-19 20:21pm    
Can you provide a sample of the data that you are getting back? From your description what they are sending back is valid
F-ES Sitecore 24-Sep-19 4:12am    
google "alphavantage javascript" and you'll find client libraries and sample code that parse the data.
[no name] 24-Sep-19 16:48pm    
I provided a sample of the data.

You may want to look at the receiver function in JSON.parse - for a trvial example, to get the time zone in your example
 var json = '{"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"}}}';

 var obj = JSON.parse(json, function (key, value) {
    if (key == "5. Time Zone") {
       alert(value);
    }
    return value;
});
would display an alert reading "US/Eastern".
 
Share this answer
 
It's not a very friendly format, but it's perfectly valid JSON, and JSON.parse won't have a problem with it.

The only issue you'll have is that you need to use a slightly different syntax to access the properties. So instead of:
JavaScript
var symbol = obj.MetaData.Symbol;
you'd use:
JavaScript
var symbol = obj["Meta Data"]["2. Symbol"];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900