View Full Version : JsonReader problem when there's a single record

22 Mar 2011, 1:27 PM
I'm facing the following problem when I try to load data using an AjaxProxy / JsonReader.
My AjaxProxy contains the following reader configuration

{type: 'ajax',
url: myUrl,
reader: {type: 'json', root: 'rootElement.records'}
Data gets loaded successfully into my store if the data returned from the server is something like this:

{"records": [ {"id": 1, "name": "abc"}, {"id": 2, "name": "def"} ] } }
However, my problem is that when there's a single record, my server app generates the data as follows, without the [ ], and in this case, the store.load() doesn't load the data, as it seems to expect an array.

{"records": {"id": 1, "name": "abc"} } }

When there are multiple records, my server app does generate the [ ] and it works fine.

How do I get around this problem when there's only a single record - without having to change the server app? Can the JsonReader be made to look at the single record without having to use an array in the data?

28 Mar 2011, 2:00 AM
It's expecting an array, not an object, so you will need to change your server-side code to output square brackets. I don't believe there's any way of forcing it to read the data the way you have it set up.

This should be relatively straightforward to change on the server-side, is there any particular reason you want to avoid doing so?

14 May 2011, 8:06 AM
There is one big reason why the reader should be able to read array-less json responses: Not all public APIs return arrays (think Twitter). So, while we could use a proxy, that means adding an extra point of failure if it's installed solely for that purpose. Of course proxies can add many benefits if you have the knowledge and resources to add and manage one.

Seems this has been discussed at length here:

and hopefully the same kind of fix will make it to Sencha Touch :)