Please note, new blog at http://www.acheron.org/darryl/

AJAX Diary: Using JSON instead of XML

In a follow up to my previous post, I am going to talk about the different ways to format data transferred from the server to the client - namely XML or JSON (JavaScript Object Notation). Most AJAX implementations focus soley on XML as the delivery mechanism for data. I think that XML is good for an envelope, but not necessarily the best choice for transmitting large amounts of data, as it is too verbose. JSON offers a lightweight format, is easily serialisable/deserialisable and maps nicely to native datatypes. In the application mentioned previously, we used a hybrid of XML and JSON. We used XML to describe the envelope (or "packet"), and JSON to "describe" the data. Anyway, in more detail, this is how it worked. Differences between XML and JSON XML is extremely versatile and an excellent mechanism of transferring data between systems. However, it does have its drawbacks. One of them is of course bloat. For example, say we were sending back a struct to the client side that had two keys, firstname and lastname. Using XML to describe the data: <response> <result>0</result> <resultstring>OK</resultstring> <data> <struct> <firstname>John</firstname> <lastname>Smith</lastname> </struct> </data> </response> Using JSON to describe the data: <response> <result>0</result> <resultstring>OK</resultstring> <data>{ firstname:"John", lastname:"Smith" }</data> </response> As you can see, the JSON version is MUCH smaller than its more verbose counterpart. This is even more true when you are considering an array of structures, or complex structures. How ColdFusion variables were converted into JSON Firstly, we created what we called a Request Broker. I'll go into more detail about this in a later post, but it essentially took the component and method as arguments and returned an XML document, with the ColdFusion variable returned by the method encoded as JSON. The Request Broker would get called like this: http://www.example.com/requestBroker.cfm?class=componentPath&method=get The ColdFusion tempalte dynamically evaluates the method passed via the URL, and also passes the FORM scope as an argument. On the client side, name/value pairs are always passed as a POST instead of a GET. This is because some variables sent to the server might be large lists or text. <cfscript> o = createObject("component", url.class); cfData = o[url.method](FORM); jsonData = jsonencode(cfData.data); </cfscript> It is the job of the component (url.class) to appropriately extract the arguments from the FORM data and then pass them as individual parameters to the Model. The output of the method is then encoded using the CFJSON UDF, written by Jehiah, and then wrapped up in an XML envelope. <response> <result>#cfData.result#</result> <resultstring>#cfData.resultString#</resultstring> <data>#jsonData#</data> </response> In my next post I'll cover the Request Broker in more detail, as well as provide a full source-code description. The idea of this post was to show you the differences between XML and JSON, and how we converted ColdFusion variables into JSON.

By Anonymous Anonymous, at 1/02/2006 03:08:00 pm  

Nicely Done ! Great....



By Anonymous Anonymous, at 1/03/2006 08:18:00 am  

we had lots of metadata (attributes) to send back with the values and that's why we were stuck with XML. no help from JSON with this, yes?

I take it you only needed the data (re: "xml bloat")?



By Blogger zwetan, at 1/03/2006 08:32:00 am  

hi, check also this post
The AJAX response: XML, HTML, or JSON?

the article and comments are interesting and in the same vein as your post.



By Anonymous Anonymous, at 1/03/2006 01:49:00 pm  

What is the deal with JSON.Com? Is it like JSON.Org or Xml.Com?



By Blogger Darryl Lyons, at 1/03/2006 06:46:00 pm  

Barry - as long as you can represent the information in a ColdFusion complex structure, then you can certainly do so in JSON. JSON allows you to build complex nested data -- e.g. array of structs, structs of arrays, etc.

zwetan - A good read. I do agree that JSON is harder to read than XML, but if the JSON is being created by a machine in the first place, then I don't think it matters once it is use. Another interesting thing is that we also used HTML snippets.



By Blogger Unknown, at 6/24/2010 01:34:00 pm  

You would become acclaimed in your acquisition just because of accustomed the best superior lv bags
with you; these cheap louis vuitton bags
are accepting added popularity. Wherever you would go, these lv bag
would be taken as a attribute of chic and elegance.



By Blogger 123, at 8/28/2010 04:01:00 pm  

http://bookmarkcircle.info/user/voted/dvd123/
http://bookmarkcircle.info/user/voted/hardysed123/
http://bookmarkcircle.info/user/voted/hermes123/
http://bookmarkcircle.info/user/voted/hermes321/
http://bookmarkco.com/user.php?login=bootpoint&view=history
http://bookmarkco.com/user.php?login=freeboots&view=history
http://bookmarkco.com/user.php?login=lv123&view=history
http://bookmarkco.com/user.php?page=3&login=tiffany123&view=history
http://bookmarkin.info/user.php?login=breitling123&view=voted
http://bookmarkin.info/user.php?login=Chanel123&view=voted



» Post a Comment