Please note, new blog at

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: 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(; </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 Mujeebur Rahmansaher, at 1/02/2006 03:08:00 pm  

Nicely Done ! Great....

By Anonymous barry.b, 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 longge, 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 longge, at 6/25/2010 01:01:00 pm  

Louis vuitton bags
accept got their own accent in our lives, for arcade or anywhere abroad after accustomed a louis vuitton handbags
with us. While traveling anywhere and the lv
is the one which makes you beautiful wherever you go. This is not alone due to the architecture of the atypical louis vuitton

By Blogger dingdang, at 7/01/2010 04:28:00 pm  

hermes handbags has been spotted on well known celebrities such as Oprah Winfrey and Martha Stewart. Popular television shows have dialogue around Kelly handbag . Find out what makes Kelly handbags unique and appealing to the rich and famous.
Along with the daidaihua , you will also receive these following products: free toning belt for your waist and abs, free quality e-books, free online exercise guidelines for you and an online customer support for you 24/7. lida , according to its consumers, lida daidaihua also has no known side effect unlike some other pills that would have side effects such as excess urination, diarrhea and constipation. Just make sure that when you take lida slimming , combine slimming capsule with a meal and exercise for effectiveness. And then watch your fats melt away. slimming capsules is best.

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

By Blogger weddingca, at 11/10/2011 06:21:00 pm  

Thanks for sharing this with all of us. Every girl has her most beautiful moment in the life just when you wear your own wedding dresses in your wedding.You can visit our wedding dress shop.Cheap wedding dresses have good quality. You can find ball gown wedding dresses,mother of the bride dresses,mermaid wedding dresses,flower girl dresses,strapless wedding dresses,a line wedding dresses,plus size wedding dresses,column wedding dresses,beach wedding dresses,empire wedding dresses,tea length wedding dresses,bridesmaid dresses,cocktail dresses,evening dresses,prom dresses your dream. Get your wedding dress in our bridal shop. Finally,I hope you have a happy wedding.

» Post a Comment


» Create a Link