View Full Version : Single element arrays in JSON?

30 Nov 2010, 7:40 AM
Is there any way for me to support single element arrays in ScriptTagProxy?

The users is the root element. Some rest services return this:

if there is multiple results, but return the users without the array if only one user.


This do not seem to work for me with Sencha Touch. Any idea how to get it to work or is it not supported?

30 Nov 2010, 2:46 PM
It's not supported, you'd need to write your own data reader. Quite odd that the service would change the output depending on the number of users.

30 Nov 2010, 11:48 PM
Hi, thanks for the answer Evant. I'll see if I can do a work around on the server side. But would be nice in the future to get this support in Sencha, since it is actually an valid case for multiple platforms.

The rest I was working against now is a Java REST client with Jersey. I know there are some work around, but they seem to be a bit of a hassle to implement.

I also know that the PHP SoapClient defaults the xml it send single elements instead of arrays if only one value. You need to specify SOAP_SINGLE_ELEMENT_ARRAYS to force PHP to keep the arrays. I know it is not exactly the same since this is SOAP and not REST, but still I think the cases are related.

1 Dec 2010, 12:24 AM
Specify the root of the JsonReader as a function.

this function will be passed the raw data object, and must return an array,

So you can examine the object to see what form it has and always return an array.

2 Dec 2010, 1:04 AM
That was exactly what I was looking for Animal, thanks!

reader: {
type: 'json',
root: function(data) {
if (data.users) {
if (data.users instanceof Array) {
return data.users;
} else {
return [data.users];

16 May 2011, 4:58 AM
This is an old thread but it's always useful.
Thanks a lot. :)

19 May 2011, 10:03 AM
This should really be added to the official documentation as I found out by accident that functions were allowed for 'root' and using the example above is a brilliant way of solving a very common problem when using public APIs. It's very useful when using associations, it saves us from having to parse the code elsewhere :).

19 May 2011, 12:43 PM
While I would think that functions, which return a value, can always replace a static value (javascript evaluates them both to a value), the fact that Sencha will actually reevaluate it each time is subtle. I used to write my own parser for the resulttext back in ext2

7 Aug 2012, 3:07 AM
Any suggestion how to solve this when using ST2 with Architect?

The root property is now depreciated and I have problems creating a function in the rootProperty property.

Or is there a better way to do this in ST2 and architect?

7 Aug 2012, 4:37 AM
reader: {
type: 'json',
rootProperty: function getData(data) { if(data.results instanceof Array) return data.results; else return [data.results];}

8 Aug 2012, 2:41 AM
That won't work since architect think it is a string and not a function...

Edit: Just to spesify it works if I do it outside architect, it is just architect that places ' around the expression.