Vishful thinking…

JSON over XML ?

Posted in Uncategorized by viswaug on January 23, 2008

I am pretty sure a whole lot of people have been faced with this question for their web applications. A while ago I was faced with that question myself. For the kind of web application I was working on, the JSON seemed like a clear winner. It was easy to work with in javascript and for scenarios like sending a dataset over to the browser, JSON seemed to be a great option to reduce the payload.

But XML is a much more mature technology and has other supporting technologies like XSLT that make working XML easier. Javascript libraries like Sarissa exist that can convert XML data from the server to HTML using XSLT transformations. XSLT can be considered a templating language’ for XML. But no such mature templating language exist for converting JSON to HTML. Libraries like JSONT have popped up but it is not a complete or well tested solution like XSLT. So, that is real big road block for blindly adopting JSON over XML right away.

Even, the existing javascript templating languages (JSONT, JSLT) are inherently slower since they make extensive use of ‘eval‘ to transform the data. Also, they get real hard to read (especially for .NET pampered developers like me). This post by John Resig goes over some metrics that will help make the JSON/XML decision. Shamelessly stealing from his post, the following graph illustrates the advantage of transferring JSON over XML data (25 – 1600 data records).

json-speeds-md

But then again, all that performance gain is blown away by the poor performance of JSONT in transforming the JSON data to HTML. The XSLT transformation to transform XML to HTML performs much better when compared to the JSONT transformation as illustrated in the graph below, again from John Resig’s post (25 – 200 data records).

jsonvxml-md

John, stops there with his analysis but I was curious as to how the performance would be affected if I wrote my own javascript to transform JSON to HTML. Does coding the transformation in javascript help the performance any? So, to test it out I added to John Resig’s test script to output metrics for transforming JSON to HTML in javascript. Get the additional test files here.

  Time (in seconds)/ Run 
Test Runs 100 200 400 1600
XML / XSLT 0.01014 0.010845 0.0116225 0.01213875
JSON / JSONT 0.01919 0.01958 0.0200075 0.02013375
JSON / Javascript 0.00421 0.004215 0.00394 0.004153125

 

The figures above illustrates that transforming JSON data to HTML by writing javascript is much faster than using either XSLT or JSONT. So, if you are willing to dish out the javascript code to do the transformation yourself that JSON seems to be a better alternative than XML.

Follow

Get every new post delivered to your Inbox.