Wednesday, April 16, 2008

Adding a Column to JSON using Coldfusion8

I use Spry to output a lot of queries that I use in my projects.

So I call my coldfusion function from Spry


var ts = new Date();
var spry_ContactList = new
Spry.Data.JSONDataSet("module_contacts.cfc?method=qry_ContactsList &returnFormat=json& queryFormat=column&TS=" + ts.toString(),{path:"DATA", pathIsObjectOfArrays:true});


There are a couple of things to note here, first of I am using Coldfusion 8's new returnFormat=json to return the query that resides in my function, as well as this I am also using queryFormat=column. Finally I have added on a date string to prevent browsers such as IE6 from caching the request.

So based on the above I can return my query as JSON, however I found that any dates were being returned in a fairly ugly way eg. March 21, 2008 00:00:00. I wanted to remove the dates and on consulting with a couple of people I came to the conclusion that doing this in Javascript would not be the best solution so I added the following after the query in my function.


<cfset tempArr = arrayNew(1) />
<cfloop query="qry_ContactsList">
<cfset arrayAppend(tempArr, "#DateFormat(qry_ContactsList.visitdate,'dd-mm-yyyy')#")/>
</cfloop>
<cfset qry_ContactsList.addColumn("PRETTYDATE", tempArr) />


This created an additional column in the JSON that was returned, and I could now display the date as I wanted to.

No comments: