0

I'd like some help with fixing this code, which attempts to return JSON that should contain something like this:

{
"col0": "4-Jan-17",
"col1": "115.85",
"col2": "116.51",
"col3": "115.75",
"col4": "116.02",
"col5": "21118116"
},
{
 "col0": "3-Jan-17",
 "col1": "115.80",
 "col2": "116.33",
 "col3": "114.76",
 "col4": "116.15",
 "col5": "28781865"
}

Here is my code as it is right now:

function getData() {
var dataFormat= "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
var site = 'http://www.google.com/finance/historical?q='+ 'AAPL' +'&startdate=Jan+01%2C+2017&enddate=Jan+05%2C+2017&output=csv';
var symbol = $("#txtSymbol").val();
var historicalQ = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from csv where url="' + site + '"') + dataFormat;


$(function() {
    $.getJSON(historicalQ, function(json) {            
         var test = json;
         console.log(test);
    });
});
}

I was originally using yahoo's database, but they recently shut that down so in this I'm trying to translate it to using Google's API. When I run this, I get something that looks completely wrong:

[object Object] {
 query: [object Object] {
 count: 4,
 created: "2017-05-24T20:26:37Z",
 lang: "en-US",
 results: [object Object] { ... }
  }
}

I know that the google link works because I have tested it in the YQL Console, so I think that the error is in my code. Any help would be appreciated!

1 Answers1

0

Turns out I figured it out. I had to look at the returned JSON response in the YQL console and found out what I needed to do. Here's what I changed:

$(function() {
    $.getJSON(historicalQ, function(json) {            
      result = json.query.results;
      var length = Object.keys(result.row).length;
      for (var i=1; i < length; i++) {
        console.log(result.row[i].col4);
      }
      });

This takes the "close" price column from the result. It was returning [object Object] and I realized that I had to specify exactly what aspect of the JSON that I wanted. Posting this just in case anyone experiences a similar issue and wants help.