On the positive side, both these formats are extremely efficient in terms of the size of the string returned. Rather than repeating them for each row, the column names are stated only once in the first type, and twice in the second.

But neither format is particularly useful when trying to pass data to jQuery plugins and other client side code, which typically expect name/value pairs.

Array conversion

Like many other developers, I generally work around this by doing a little extra work on the server side to convert the query object to an array. CF will then serialise that as name/value pairs. This also allows you to circumvent another notorious CF quirk in which it forces all keys to upper case (a royal pain with case-sensitive Javascript). Using bracket-notation as you convert the query prevents this.

With this tiny sample array, the JSON size is actually smaller than that of either of the query formats, but because the column names are repeated for each row, you can see how with larger data sets a lot of redundant data would be transferred from server to client.

What if we could get the best of both worlds: the efficiency of CF's JSON format as it's transferred, but the straightforwardness of the name/value format as it's consumed?

serializeCFJSON

Enter Steve's serializeCFJSON jQuery plugin which will take the CF query JSON as delivered over the network and massage it into the name/value format ready to be used.

At the time of writing though, Steve's code only appears to work with queries nested inside larger JSON blocks, so I created a simpler version for use exclusively on single, non-nested queries:

(Note that the function forces the upper case keys to be lower case. A downside of this approach is that we don't get the opportunity to preserve the original case, but lower case will normally be preferable to upper.)

UPDATE 8 May 2012: Steve has updated his plugin to work with non-nested queries.

Putting it all together, we can now simplify our remote cfc method which no longer needs the array conversion and can return the query directly as JSON:

In your console you should be able to see both the original JSON as sent over the network, and the converted name/value pairs ready for consumption.

Making the appropriate choice

Although it's more work on the server-side, the array conversion technique does give you more control over the data, especially when it comes to preserving case. But client-side conversion of the original CF query format is worth considering if you expect large data sets and minimising network overhead is important.

Comments

Want to paste code? Enclose within <pre><code> tags for syntax higlighting and better formatting and if possible use script. If your code includes "self-closing" tags, such as <cfargument>, you must add an explicit closing tag, otherwise it is likely to be mangled by the Disqus parser.