Quick note about my serializeCFJSON project out on GitHub. I wrote this quick JQuery plugin to convert ColdFusion's JSON representation of it's native query objects. ColdFusion represents datasets in a trim manner, as an object containing two arrays: One, an array of column names, and the other, an array of arrays, each representing one record of the set. Most representations of recordsets are an array of objects, each record represented as a collection of name/value pairs. ColdFusion's representation is much smaller, removing a lot of unnecessary duplication, but many pre-built frameworks and plugins look for the name/value pair objects.
What the plugin does is convert ColdFusion's representation into the more standard form. Ajax transfer is unaffected, as the trimmer format is still being passed. What we get is some very minor client-side overhead in the creation of a new object. What's more, the plugin recursively searches through a JSON object and converts any ColdFusion query that it finds. So, if you nest your query inside a larger object, the plugin will still convert it for you.
I put up a demo in the Projects menu, at the top of this site. This is a refactor of my Grouped jqGrid example, using the plugin for the data conversion. The full sample code for the demo can be found in the GitHub repository.
Take her out for a spin, and let me know what you think.


#1 by henryho167 on 5/3/12 - 8:09 PM
#2 by Cutter on 5/3/12 - 9:12 PM
While that is true, it's not relevant for our use case. In this use case, adding returnFormat=true to the ajax request forces ColdFusion to serialize the JSON, rather than having it as part of the method. the SerializeJSON() ColdFusion method has it's uses, but in this use case you are able to write CFC methods that return standard CF data types, and therefore be reusable across your application, server-side or remote request.
For further info, see the blog post I referenced above about nesting your query inside a larger object.
#3 by Julian Halliwell on 5/6/12 - 9:56 AM
I couldn't get your plugin to work though until I realised it is actually designed to handle the alternative JSON format that CF produces if you use the queryFormat="column" parameter (or the function + arg Henry mentions), rather than the default format for a query returned as JSON, which is what you describe in your post and docs.
#4 by Julian Halliwell on 5/6/12 - 2:09 PM
var data = {
"COLUMNS":["ID","NAME","EMAIL"],
"DATA":[[1,"Ed Spencer","ed@sencha.com"],[2,"Abe Elias","abe@sencha.com"],[3,"Cutter","no@address.giv"]]
};
console.log( $.serializeCFJSON( q ) );
But if I create another object, attach the query data to it and then pass that to your function, it does work:
var data = {
"COLUMNS":["ID","NAME","EMAIL"],
"DATA":[[1,"Ed Spencer","ed@sencha.com"],[2,"Abe Elias","abe@sencha.com"],[3,"Cutter","no@address.giv"]]
};
var temp = new Object();
temp.data = data;
console.log( $.serializeCFJSON( temp ) );
#5 by Julian Halliwell on 5/6/12 - 3:45 PM
console.log( $.serializeCFJSON( q ) );
in the first example should have been
console.log( $.serializeCFJSON( data ) );
#6 by Cutter on 5/8/12 - 9:09 AM
Thanks for the heads up. I must've missed that in testing. I've just loaded v0.2 to the GitHub repository, which addresses the issue.
#7 by Erik on 5/29/12 - 1:18 AM
I have been trying to implement this plugin but I get every character of my json as an individual element. Are there any more examples for the use of thisplugin apart from the github?
Looks like a fantastic plugin.
Erik
#8 by Cutter on 5/29/12 - 8:15 AM
I'm sure there are, but I couldn't tell you off the top of my head. Can you send me some sample code? Copy and paste your ajax response out of Firebug? If you can do that (through the contact form) I'll see what I can do to help you.
#9 by Franc on 11/14/12 - 10:06 PM
success: function (json) {
console.log($.serializeCFJSON({data:json}));
}
#10 by Cutter on 11/16/12 - 10:10 AM
Sounds like your ajax config is missing something. Can you hit the contact form, and send me an example of your full ajax request? It might be something simple. (might want to send a sample of your request return as well)
#11 by Scott on 12/21/12 - 9:43 AM
var populateGrid = function (postdata) {
$.ajax({
url:'/components/orders.cfc',
data: postdata,
method:'POST',
dataType:"json",
success:function(d,r,o){
var s;
if(d.success){
grid[0].addJSONData($.serializeCFJSON(d));
} else {
console.log('didnt load data');
}
}
})
};
grid.jqGrid({
autowidth: true,
height: 250,
loadui: 'block',
altRows: true,
deepempty: true,
pager:'##pager',
toppager: false,
pagerpos:'left',
rowNum:50,
rowList:[10,20,30,40,50],
viewrecords: true,
colModel: [
{name:'date_placed'},
{name:'id_order'},
{name:'po'},
{name:'totalparts'},
{name:'total'}
],
postData {method:"getOrdersProxy",returnFormat:"JSON",customerId:"2851"},
datatype: populateGrid,
jsonReader: {
id: "id_order",
root: function(obj){return obj.data;},
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.recordCount; },
cell:""
}
});
});
My cfc is just returning a simple nested query as in you examples.
#12 by Michael Sumner on 2/24/13 - 2:15 AM
I tried your serializeCFJSON but got this error
TypeError: invalid 'in' operand obj
[Break On This Error]
typeof length === "number" && length > 0 && ( length - 1 ) in obj );
jquery.js (line 965
I got the json that cf sent logged to the console, I included the
jquery.serializecfjson-0.2.js right after jquery
Your function is being called in the success of a form submit
$.ajax({
type: "POST",
url: "/cfc/show_specials_Service.cfc?method=submitSS",
data: dataString,
dataType: "xml",
success: function(data){
console.log(this.data);
/*alert('this works');*/
$.ajax({
url: "/cfc/show_specials_service.cfc?method=getSSS",
success: function(response){
console.log(response);
var ret = $.serializeCFJSON(response);
console.log(ret);
Maybe you can see something I missed.