When we first decided to include AJAX functionality in CF8, well, that was just about all that we had decided! Which was fun, because it gave me a broad mandate to go out and come up with some crazy ideas, and scary, because I didn’t have a clue what to do about it.
So we started small, and built native support for JSON into the server, with a fast Java JSON parser of our own, which provided the basis for the new JSON functions in CF8:
serializeJSON(<CFML object>, <serializeQueryByColumns>) deserializeJSON(<JSON string>, <strictTypes>) isJSON(<JSON string>)
I’m not going to get into the specifics of these functions here - Ben Nadel’s done a great job covering them on his blog. Also, a note, these function signatures are not final - we’re tweaking them a bit (just a bit, applications you write now will not be broken!) for the final release. But more on that later.
JSON has been gaining currency as an alternative to XML for data interchange for a variety of reasons: it’s lighter, easier to read and write, maps well to basic constructs in many languages (such as arrays and structs), and, perhaps most importantly for the dynamic web, plays well with yer average browser’s language of choice, JavaScript.
With just these basic functions, it becomes easy to consume JSON-powered web services, mash them up as you will, and spit them back out again. For instance, here’s a tasty morsel of a CFC I wrote to get data from del.icio.us JSON web services:
<cfcomponent output="false"> <!--- Gets del.icio.us tags for a user. The web service returns a JSON object, mapping tag names to numbers of posts with that tag, which deserializes to a CFML Struct. More details of the web service available at: http://del.icio.us/help/json/tags ---> <cffunction name="getTags" access="remote" output="false"> <cfargument name="userid"> <cfset tagsURL = "http://del.icio.us/feeds/json/tags/#userid#?raw"> <cfhttp url="#tagsURL#"> <cfreturn deserializeJSON(cfhttp.fileContent)> </cffunction> <!--- Gets posts for a user, optionally for a specified set of tags. The web service returns a JSON array of objects, one object per post. Each post object has the keys: u - URL d - description t - tags This deserializes to a CFML Array of Structs, with one Struct per post. More details of the web service available at: http://del.icio.us/help/json/posts ---> <cffunction name="getPosts" access="remote" output="false"> <cfargument name="userid"> <cfargument name="tags"> <cfset var tagList = replace(tags, ",", "+")> <cfset tagsURL = "http://del.icio.us/feeds/json/#userid#/#tagList#?raw&count=30"> <cfhttp url="#tagsURL#"> <cfreturn deserializeJSON(cfhttp.fileContent)> </cffunction> </cfcomponent>
CFHTTP to the service URL, deserializeJSON on the response, and, all of a sudden, you have native CFML types to extract data from. Doesn’t get much easier than that! I’ll show off an AJAX application that I wrote with this CFC in a little while.
Given CF’s ability to consume data from a variety of sources (databases, LDAP servers, FTP, MS Exchange, HTTP, etc.), along with native support for common data formats (XML, and now JSON), and the large variety of web services out there, you’re now limited only by your own imagination in the mashup applications you can build. Go forth and multiply!

Stake Five :: CF8: Evolving JSON Support, Part 2 | 01-Jul-07 at 7:54 am | Permalink
[...] my last post, I looked at the new JSON functions available in ColdFusion 8, and showed how they could be used to [...]