Example: Simple YQL Query

In this example, we make a simple YQL Query to retrieve data from the Yahoo! Weather YQL table.

Querying YQL..

The easiest way to build a YQL query is by visiting the YQL Console. In this example we will be using the weather.forecast table. The YQL statement that we are using looks like this:

select * from weather.forecast where location=90210

You can preview this query in the YQL Console to get a sense of the data it returns.

Setting Up the YUI Instance

Now we need to create our YUI instance and tell it to load the yql and node modules.

YUI().use('node', 'yql');

Making the Query

We now have a YUI instance with the yql module (and its dependencies) attached, so we can proceed to make a query.

YUI().use('node', 'yql', function(Y) {
    
    var res = Y.one('#res'),
        zip = '90210';
    
    Y.YQL('select * from weather.forecast where location=' + zip, function(r) {
        var el = Y.Node.create('<div class="mod"></div>');

        el.set('innerHTML', '<h2>Weather for ' + zip + '</h2>' + r.query.results.channel.item.description);

        res.setHTML(el);
    
    });
});