0

I'm very new to Javascript and JQuery, I've been trying different ways to pull and manipulate yahoo finance data, and have decided on jquery. My first basic attempt was this:

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20Name%2C%20LastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22RHT%22%29&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env" + "?callback=?", function(json) {

var lastprice = json[0].results.quote.LastTradePriceOnly

console.log(lastprice)

It did not work, and the error console was not of any help. I searched here and found this question: load json into variable and tried this after thinking maybe the response hadn't yet been received from yahoo:

var json = (function () {
    var json = null;
    $.ajax({
        'async': false,
        'global': false,
        'url': "http://query.yahooapis.com/v1/public/yql?q=select%20Name%2C%20LastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22RHT%22%29&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env",
        'dataType': "json",
        'success': function (data) {
            json = data;
        }
    });
    return json;
})(); 

    var lastprice = json.results.quote.LastTradePriceOnly

    console.log(lastprice)

});

This too wasn't right. I feel like I'm close. Any help would be greatly appreciated

Community
  • 1
  • 1
majordomo
  • 1,160
  • 1
  • 15
  • 34

2 Answers2

2

Remove the callback for the url and you get the following JSON result from the query, which is not an array but an object.

{
   "query":{
      "count":1,
      "created":"2012-10-26T19:00:18Z",
      "lang":"en-US",
      "results":{
         "quote":{
            "LastTradePriceOnly":"50.26",
            "Name":"Red Hat, Inc. Com"
         }
      }
   }
}

You should acces its data in the following way:

var lastprice = json.query.results.quote.LastTradePriceOnly;

Here's a working demo for the modified code: DEMO.


EDIT (WARNING)

I was testing that call and I'm seeing that sometimes the result from that service with the same URL returns the following error:

{
   "error":{
      "lang":"en-US",
      "description":"No definition found for Table yahoo.finance.quotes"
   }
}
juan.facorro
  • 9,791
  • 2
  • 33
  • 41
  • I was half-way through the same answer when you beat me too it. For reference to the OP, try pasting the url into [JSONLint](http://jsonlint.com/#) to check the format. – Matt Burland Oct 26 '12 at 19:05
0

data will come back in this format:

{"query":
       {"count":1,"created":"2012-10-26T19:00:42Z","lang":"en-US","results":
          {"quote":
              {"LastTradePriceOnly":"50.28","Name":"Red Hat, Inc. Com"}}}}

so the object you get back will have to be traversed : data.query.results.quote.LastTradePriceOnly

>     $.ajax({
>         'async': false,
>         'global': false,
>         'url': "http://query.yahooapis.com/v1/public/yql?q=select%20Name%2C%20LastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20%28%22RHT%22%29&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?",
>         'dataType': "json",
>         'success': function (data) {
>             alert(data.query.results.quote.LastTradePriceOnly);
>         }
>     });

Here is a jsfiddle which demonstrates it: http://jsfiddle.net/erick382/Qghtu/

ek_ny
  • 10,153
  • 6
  • 47
  • 60
  • Thank you (feel dumb for getting that path wrong), I tried adding a variable to what you wrote by changing the end to this (but it didn't work): 'success': function (data) { json = data; } var last = data.query.results.quote.LastTradePriceOnly console.log(last) }); – majordomo Oct 26 '12 at 19:39
  • Hmmmmm... Did you look at the jsfiddle? I know I simplified it.. But it might help you troubleshoot.. – ek_ny Oct 30 '12 at 16:34