The Power And Versatility of JSON (Over XML)By Angsuman Chakraborty, Gaea News Network
Monday, July 16, 2007
It is never a good idea to mix syntax with semantics. When comparing JSON with XML this is what first came to my mind. XML provides a syntactical as well as a semi-semantical layer for data transfer. And that makes it a complicated choice. But that is just the tip of the proverbial iceberg. There is more to the story and at stake is the simple way of doing things.
XML was simple to begin with. But then the semantics crowd started to get involved. Soon XML devolved into the awful bloat of technology that it is today with all these namespace and URI.
Ode To JSON’s Simplicity
Parsing JSON documents is simple in whatever language you choose. Writing it is simpler and faster compared to XML. Personally I hate writing the < & >’s just to send a simple attribute or two across the wire. Reading a JSON document is easier to the eye. JSON is also more compact than XML.
When transferring small data over the wire I would heartily recommend JSON to anyone who would listen.
XML too has an enabling technology named Apache Axis. If you had ever written simple web services with Apache Axis you will appreciate at its simplicity and power. With such enabling technologies, think JDOM too, it can very much empower the users, there is not doubt. However the default technologies that comes with XML are SAX (pronounced like Sucks) and DOM. They are not at all fun to work with.
Zen and the Art of Data Transfer
Have you ever wondered why all the popular data transfer protocols are human readable? Take as examples FTP, HTTP, SMTP, POP3 etc. even JSON and XML. There is a solid reason behind this. Data transfer is error prone and I am not just talking about the error introduced in the wire. Modern protocols like TCP mostly take care of errors in the wire. The challenge is in ensuring that you are transferring the data in proper format which is properly understood by the receiving party. Mistakes are bound to happen. A human readable protocol allows you to debug it easily and fix it.
Try reading a reasonable complex document (like genomic annotations) in XML and it will appear to you as mostly unreadable (or readable in a Matrix, the movie, kind of way). JSON on the other hand closely resembles the data structure which humans are comfortable to interpret. In fact if I were explaining a data structure I would henceforth use JSON syntax.
I tried recently debugging huge amount of data I was sending over the wire in JSON. It took me a fraction of the time to debug it ( 5 minutes or less) than it would have taken me to debug an equivalent XML document. Human eyes, in my humble experience, do not adjust well to <’s and >’s. They confuse the hell out of my poor eyes. If I were to compare it with program debugging I would say XML debugging is like using Borland C++ 4.5 (remember the switching between console and GUI mode with every line of instruction?) for debugging, whereas JSON debugging is like debugging with VC++ 2.0.
What about semantics?
As I said before don’t mix syntax with semantics. If you really wish to attach universally understandable semantics to your document then layer it on top of a simple syntactical layer like JSON. It will make both camps happy. It will make for easier debugging. It will also allow you to move your semantics to a different data transfer layer easily. Most of the time however the need for universality is in your head. Forcing semantics where it doesn’t need to be, forces you to adopt laughable semantics like foaf.
The bottom line is that if you need to transfer some simple data then don’t waste your time over XML, use JSON. You will be more effective as a developer and your boss will love (platonic I hope) you for it.
Tags: Eye, Eyes, Fact, FOAF, JSP, Say, XML