0

I'd like to use this code to parse JSON data from a Yahoo finance CSV file. The JSON link is valid, I'm just having trouble with the function (data) and var data lines. The parse table calls for function (data) but that conflicts with the var data line. The original code I used to parse the table had used function (json), but this specific api seems to only work with the function (data).

function drawTable() {
// use jQuery to make an AJAX request for data
$.ajax({
    type: "get",
    dataType: "jsonp",
    url: 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quoteslist%20where%20symbol%3D%27BRDT%2CAPPL%27&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=',
    success: function (data) {
        var data = new google.visualization.DataTable();

        data.addColumn('string', 'Symbol');
        data.addColumn('number', 'open');


        // parse the JSON into the DataTable
        for (var i = 0; i < json.list.resources.length; i++) {

            var symbol = json.list.resources[i].resource.fields.symbol;
            var open = parseFloat(json.list.resources[i].resource.fields.open);


            data.addRow([symbol, open]);
        }

        var table = new google.visualization.Table(document.querySelector('#table_div'));
        table.draw(data);
    }
});
}
google.load('visualization', '1', {
    packages: ['table'],
    callback: drawTable
});
musclez
  • 113
  • 1
  • 14
  • Describe the problem you are having in detail. "I'm having trouble with a line" isn't descriptive enough. – Axel Jul 23 '14 at 19:24
  • I combined two pieces of code I found which have conflicting functions, one being fucintion (data) and the other being function (json). A working example of the code I modeled this after in the post now. _jpugliesi_ has hinted at my error, but the solution he suggests doest not solve the issue. – musclez Jul 23 '14 at 19:40

2 Answers2

1

It looks as though you're passing the success: function(data) function a variable for the JSON data called data. Then immediately within the success function you're instantiating an entirely new data variable with a DataTable object, thus overwriting the previous data variable containing the returned JSON data. (line: var data = new google.visualization.DataTable();)

I would try changing the name of that var data variable to something else, say, data_table, though I haven't actually tried making this change to see if it works.

Hope this helps!

jpugliesi
  • 26
  • 4
  • I tried your method but no dice. [This](http://stackoverflow.com/questions/12250065/receive-csv-file-as-data-in-ajax-success-function) is an example why I chose to switch the functioned variable to `data`. From my understanding, using `function(data)` alongside the `type: "get",` and `dataType: "jsonp",` allows a google api to be used with the same code, which was the primary goal from my original [post.](http://stackoverflow.com/questions/24855953/stock-watchlist-with-google-finance-json-api) More information on usage of both api's with a JSON request can be found there. – musclez Jul 23 '14 at 20:16
1

I changed a couple of variables names and remove the 's' from https on the url, also set diagnostics to false since it was throwing weird symbols.

Hope this is what you were looking for

demo

function drawTable() {
    // use jQuery to make an AJAX request for data
    $.ajax({
        type: "get",
        dataType: 'json',
        url: 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quoteslist%20where%20symbol%3D%27BRDT%2CAPPL%27&format=json&diagnostics=false&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=',
        success: function (data) {
            var dt = new google.visualization.DataTable();
            dt.addColumn('string', 'Symbol');
            dt.addColumn('number', 'Open');

            // parse the JSON into the DataTable
            for (var i = 0; i < data.query.results.quote.length; i++) {
                Symbol = data.query.results.quote[i].Symbol;

                Open = parseFloat(data.query.results.quote[i].Open);

                if (isNaN(Open)) {
                    Open = 0;
                }

                dt.addRow([Symbol, Open]);
            }

            var table = new google.visualization.Table(document.querySelector('#table_div'));
            table.draw(dt);
        },
        error: function (e, a, s) {
            alert(s);
        }
    });
}
google.load('visualization', '1', {
    packages: ['table'],
    callback: drawTable
});
Juan
  • 4,910
  • 3
  • 37
  • 46