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
Remove the callback for the url and you get the following JSON result from the query, which is not an array but an object.
You should acces its data in the following way:
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: