Why JSON beats XML

Last summer I had the opportunity to implement a library that serialized Java objects to JSON strings and de-serialized JSON strings to Java objects. I know I should have used one of the umpteen existing libraries – but hey, I was suffering a temporary bout of NIH :-)

When doing this work, I realized how JSON has really hit upon the sweet spot of data interchange. There are really only two data structures that matter for any sort of data interchange between applications – arrays and hash maps (I’m including any arbitrary combination of the two – for e.g. a hash map of arrays of hash maps). By focusing on these two cases alone and providing a very compact representation, JSON has really nailed the problem of simple and efficient data interchange between applications.

Wasn’t XML supposed to solve this problem ? Well, XML is neither simple nor efficient from a data interchange perspective.

Schema introduces serious complexity when using XML for representing data while not serving any really useful purpose as far as I can see. Most of the issues with dealing with data from foreign sources have to do with interpreting the semantics of the data. Schema only helps with hints about the structure of the data; this is marginally useful and definitely not worth the huge effort involved in defining Schema. Only in the rare case where a certain structure is very widely accepted and used does Schema even begin to make sense.

Now, one could argue for using XML without Schema and I am willing to bet good money that most of the use of XML in the real world is without Schema. But in this case, the other failing of XML – that of efficiency creeps in. XML is too verbose because it was designed to be very general.

JSON has another huge advantage – besides its origins in JavaScript, it has a natural affinity with dynamic languages like PHP, Ruby and Python. Arrays and hash maps are very widely used in programs written in these languages – therefore dealing with foreign data (de-serialized from JSON strings) is no different than dealing with local data. No specially convoluted and over-engineered APIs to learn or use. XML, try and beat that :-)

3 Responses to “Why JSON beats XML”

  1. Don’t forget Groovy.

  2. Shankar, you might want to check out my blog; I have written about JSON, in a similiar light, a number of times. http://www-03.ibm.com/developerworks/blogs/page/pmuellr

  3. AllInTheFamily says:

    Check out the Lua programming language. The only data structure in Lua is a Table which is a array + hashmap.

Leave a Reply