Twitter: raymondcamden


Address: Lafayette, LA, USA

What is the "best" form to return different types of data?

04-29-2014 3,225 views Development 1 Comment

I'm helping a friend deal with an AJAX issue and ran across something he was doing that was - in my opinion - a bit wrong. Specifically it involved how he was returning complex data from a remote service. I thought I'd whip up some quick advice about this. While his issue involved AJAX, this advice should really apply to just about anything, and as always, I assume folks have their own opinions/suggestions as well. Add 'em to the comments below!

One value

Returning just a simple value? Cool. Just return it. That was easy.

A set of values

Return an array. Boom, easy too.

A set of sets

OK, now this is why I wrote the blog post in general. He was returning a set of results where each individual result was also a set of values. His mistake, in my opinion, was the form of his result. His result data looked like this.


[["ray",41,true,39],["bob",39,true,21],["joe",52,false,48]]

So what exactly is that? In this case the data represents a list of people. He used an array of arrays where each person was an array and the individual person data was also array elements. The first item is the name, the second is their age, the third was a boolean indicating their job status, and the final result was the number of books they read last year.

No wait... sorry... the 4th item is the age and the 2nd item is the number books.

No... shoot... see the problem? A better solution would be an array of maps (or structs):


[{"name":"ray","age":41,"job":true,"books":39},{"name":"bob","age":39,"job":true,"books":21},{"name":"joe","age":52,"job":false,"books":48}]

Woot - much better! Of course, it is also about twice as big too, so it isn't always straightforward. If you are dealing with AJAX then this should be a concern. How much data should you expect your service to normally return? Do you have a limit on the server side? It is entirely possible you do want to send a few thousand results (perhaps as part of an initial data seed) and in that case, using the array of arrays might be better.

p.s. While I was specifically helping him with a ColdFusion problem, I didn't write this post to be specific to ColdFusion. I do want to point out that ColdFusion 11 offers a new way to return query data - one much like the array of structs above. It too is a lot easier to deal with than the "normal" way ColdFusion returns a serialized query. But the normal way is also a lot slimmer, much like the array of arrays was as well. There was definitely some logic to how it was done.

1 Comment

  • Commented on 05-02-2014 at 12:00 PM
    One issue that I've run into in the past is that, when you want to return an integer via AJAX, that can give you trouble on the JavaScript side. I've had to encapsulate the integer when I'm returning it in the past, but it may have been due to how I was doing it, whether I was returning it in plain format or not, etc.

    I am not a fan of arrays that are intended to be structs, particularly mixed-type arrays. I expect the mixed-type arrays issue is something that I inherited from my strongly-typed background in C/C++. The arrays pretending to be structs issue is simply a readability issue: If I can't look at the code a year from now and immediately understand what's going on, something must change.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty