0

Arrays of size 1 are removed in json

CODE
use 'http://paulisageek.com/tmp/yql-array.xml' AS yql-array; SELECT * FROM yql-array;


The javascript is

CODE
var awards = [];
var result = {"award" : awards};

awards.push({"a" : "b"});
if (Math.random() > 0.5) {
awards.push({"c" : "d"});
}

response.object = result;


And the result is sometimes :

CODE
"results":{
"result":{
"award":{
"a":"b"
}
}
}


and other times

CODE
"results":{
"result":{
"award":[{
"a":"b"
},
{
"c":"d"
}
]
}
}


I expected :

CODE
"results":{
"result":{
"award":[{
"a":"b"
}]
}
}


in my parser.

by
1 Reply
  • QUOTE (Paul Tarjan @ Aug 26 2009, 12:24 AM) <{POST_SNAPBACK}>
    CODE
    use 'http://paulisageek.com/tmp/yql-array.xml' AS yql-array; SELECT * FROM yql-array;


    The javascript is

    CODE
    var awards = [];
    var result = {"award" : awards};

    awards.push({"a" : "b"});
    if (Math.random() > 0.5) {
    awards.push({"c" : "d"});
    }

    response.object = result;


    And the result is sometimes :

    CODE
    "results":{
    "result":{
    "award":{
    "a":"b"
    }
    }
    }


    and other times

    CODE
    "results":{
    "result":{
    "award":[{
    "a":"b"
    },
    {
    "c":"d"
    }
    ]
    }
    }


    I expected :

    CODE
    "results":{
    "result":{
    "award":[{
    "a":"b"
    }]
    }
    }


    in my parser.


    This is because YQL turns your response.object JSON into XML for further processing in the engine (such as additional WHERE local filtering and projection using SELECT). Unfortunately when it turns it back to JSON right at the end it has no idea whether the element was part of a list or a singleton - so it "looses" its array quality and produces this undesirable effect. The same behavior can be seen when accessing a single result from a web service vs multiple results from the same web service.

    Jonathan
    0

Recent Posts

in YQL