0

Bug? or just poor design?

I'm writing an app to track a portfolio and I found the YQL stuff to be fantastic. I wrote some PHP wrapper functions to collect the information I was looking for and everything works swimmingly... until... it turns out that the output format in JSON is different when multiple results are returned vs a single result, and that is rather baffling to me. Why in the world would it be designed like this?

It looks like the JSON parser is not intelligent to recognize the resultant rows as an array if there is only one row. I cannot even imagine how that could be the case; even my browser's Javascript engine does that with ease!

I give an example below:

select * from yahoo.finance.stocks where symbol IN ("yhoo", "goog")
CODE
{
"query": {
"count": "2",
"created": "2010-09-07T12:02:51Z",
"lang": "en-US",
"results": {
"stock": [
{
"symbol": "yhoo",
"CompanyName": "Yahoo! Inc.",
"start": "1996-04-12",
"end": "2010-NaN-07",
"Sector": "Technology",
"Industry": "Internet Information Providers",
"FullTimeEmployees": "13900"
},
{
"symbol": "goog",
"CompanyName": "Google Inc.",
"start": "2004-08-19",
"end": "2010-NaN-07",
"Sector": "Technology",
"Industry": "Internet Information Providers",
"FullTimeEmployees": "21805"
}
]
}
}
}


select * from yahoo.finance.stocks where symbol IN ("yhoo")
CODE
{
"query": {
"count": "1",
"created": "2010-09-07T12:04:52Z",
"lang": "en-US",
"results": {
"stock": {
"symbol": "yhoo",
"CompanyName": "Yahoo! Inc.",
"start": "1996-04-12",
"end": "2010-NaN-07",
"Sector": "Technology",
"Industry": "Internet Information Providers",
"FullTimeEmployees": "13900"
}
}
}
}


As a user of an API, I now have to plan parsers for two scenarios (one for multiple rows, one for a single row) for something that should, ultimately, be completely identical, structure-wise. What was the thought process behind this? Is it an oversight, a design, is there an argument I can pass to make the result set consistent? I anxiously await feedback.

Jeremy Holovacs

by
4 Replies
  • QUOTE (Jeremy @ Sep 7 2010, 04:26 AM) <{POST_SNAPBACK}>
    As a user of an API, I now have to plan parsers for two scenarios (one for multiple rows, one for a single row) for something that should, ultimately, be completely identical, structure-wise. What was the thought process behind this? Is it an oversight, a design, is there an argument I can pass to make the result set consistent? I anxiously await feedback.

    Jeremy Holovacs


    Man, so I am not crazy? I though I've just dreamt it, It's like that, and even I am a noob I found it a VERY inconsistent design!
    0
  • Hi Jeremy,

    What I see there is a classic problem transforming xml into json.

    For the query:
    select * from yahoo.finance.stocks where symbol IN ("yhoo")

    You just have a single element <stock> in the xml response, so the automatic parser will create a single json object.

    When we use the query:
    select * from yahoo.finance.stocks where symbol IN ("yhoo", "goog")

    You will have two elements <stock> in the xml response, to this time the parser (xml to json parser) when it finds more than one xml element it creates an array of json objects.

    So basically, is not a bad design, I will say that is the behavior of using an automatic tool.
    Also this shouldn't affect at all your code, just detect the type of the element (typeof()), and use always the array approach, even when you don't have it, create one :DFrancisco.
    0
  • QUOTE (arcturus @ Sep 21 2010, 02:12 PM) <{POST_SNAPBACK}>
    So basically, is not a bad design, I will say that is the behavior of using an automatic tool.
    Also this shouldn't affect at all your code, just detect the type of the element (typeof()), and use always the array approach, even when you don't have it, create one :DLeo
    0
  • Hi Groob!

    Sorry, I though you were using javascript, in php should be really easy checking if you have an array or not with the function 'is_array'.

    Cheers!
    F.

    QUOTE (Groob @ Sep 24 2010, 11:49 AM) <{POST_SNAPBACK}>
    Hi francisco, a couple of questions:
    I think in php there is no "typeof" but just "gettype" but I'm not totally sure about it. Then, for example, in this case
    CODE
    	$ch = curl_init($yql_query_url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($ch); /*RESULT of YQL QUERY*/

    $type=gettype($json);
    echo $type; //it will always return a type "String"

    /*Convert JSON to PHP object*/
    $phpObj = json_decode($json);

    $type=gettype($phpObj);
    echo $type; //it will always return a type "object"

    /* Confirm that results were returned before parsing*/
    if (!is_null($phpObj->query->results)) {
    /** If the are more than one results they will be in an array that I need to cycle through **/
    if ($phpObj->query->count != 1) {
    Do Something...
    }
    else {
    Do Something Else...
    }


    In this case I'm converting the json in a php object, what should I check to avoid having 2 routines whether or not I have a single result or multiple rows?
    Thanks
    Leo
    0

Recent Posts

in YQL