javascriptjsonrestapigoogle-finance-api

Google Finance API


I am trying to test the price from "l" in the get request to the url http://finance.google.com/finance/info?client=ig&q=NASDAQ:AAPL

This is my code. I keep getting error JSONError: Unexpected token '/' at 2:1 // [ ^

var jsonData = [];
jsonData = JSON.parse(responseBody);
jsonData = jsonData.Replace("//","");
tests["Google Stock"] = jsonData["l"] === 157.50;

Solution

  • It's because you're running JSON.parse on the responseBody, that starts with '//' ... you simply need to replace the first 3 lines with this

    var jsonData = JSON.parse(responseBody.replace('//', '');
    

    Note: it's replace not Replace

    and then

    tests["Google Stock"] = jsonData[0]["l"] === 157.50;
    

    because the data is an array of objects (single item, but still an array) -the above is better written

    tests["Google Stock"] = jsonData[0].l === 157.50;