Search code examples
arraysjsonyql

Get nth row from JSON array generated by YQL


I'm using YQL to get and display data from a Google spreadsheet, like this:

http://jsfiddle.net/weCve/1/

I'd like to display results from a composite YQL query of multiple queries, and show the results in separate unordered lists.

My composite query is here. and a fiddle is here: http://jsfiddle.net/svh2r/1/

My question is, what do I have to do to this line in order to get it to take a certain row?

$.each(data.query.results.results.row, function (i, item) {

I thought it would be

data.query.results.results.row[1]

but that doesn't work.


Solution

  • data.query.results.results is an array containing each of the result sets from the separate queries. You can access them individually via data.query.results.results[n] where n is 0, 1 or 2 (corresponding with your three queries).

    To loop over the results from the second query (offset 1 in the results array) you could do:

    $.each(data.query.results.results[1].row, function (i, item) { … });
    

    It may (or might not) be useful to use two $.each loops to loop over all of the rows from all of the results.

    $.each(data.query.results.results, function (i, results) {
        $.each(results.row, function (i, item) {
             // do something
        });
    });
    

    Here is an adaptation of your example code, altered to build three lists — http://jsfiddle.net/XfBsj/