Feature Post

Top

AJAX or AJAJ: XML vs JSON

Why JSON? One word: Simplicity.

More than one word: JSON is simple, simpler than Xml. I am big fan of Xml, and that makes me a love XPath as well. But this does not mean that I am against JSON or otherwise. While on my way back home the other evening, this guy who works in the IT department of one of my customer, where I was for the project implementation. While he was talking to "his" friend(colleague, probably) about JSON and Xml, and if Xml is there then why would you ever use JSON? I was sort of magnet'ically attracted to the discussion and eventually became the part of it. So, here I thought I should add my two cents of understanding of how and where I would use either and what would be the use case.


Xml is more "documented" oriented, has its own defined format; you may use Xml in cases where structured documents are required - and that with a large amount of data. While JSON is more like "data oriented", which makes JSON handy for lightweight data exchange.

The purpose of JSON and Xml may be the same - data exchange; but the usage scenario for each is different. For instance you cant use a mallet, where a ball-pein is required.
"Ajax is a technique used for building interactive web applications that provide a snappier user experience through the use of out-of-band, lightweight calls to the web server in lieu of full-page postbacks. These asynchronous calls are initiated on the client using JavaScript and involve formatting data, sending it to a web server, and parsing and working with the returned data. While most browsers can construct, send, and parse XML, JavaScript Object Notation (or JSON) provides a standardized data exchange format that is better-suited for Ajax-style web applications." - Writes Atif Aziz
JSON is more handy as compared to Xml when there a browser-server communication is desired. Plus its,
  • Similar to Xml human/machine readable format
  • Both has support for Unicode
  • "Self-documenting format" - that is, describes the structure, fields, and values
  • Mostly used in cases where you have to maintain a list, or arrays, or trees, or records.

This could mean that JSON may become handy and lightweight way of data exchange if you think about how to use it within jQuery based AJAJ calls.

Some of the things that people take as benefit is that, to avoid name conflicts Xml has namespaces; while in JSON there is no namespace; but the URI that you fetch the JSON document from implicitly becomes the namespace.

With Xml you have to think 'DOM'ically; but in JSON the data is plain, simple and easy to read.

JSON does not support data validation; is not extensible.

So, conclusion - whatever you use, choose it based upon what is required.
"JSON is a data format, but one which is more naturally fit for browser data consumption. JSON is a subset to JavaScript, and by structuring a data payload as a JSON response, you are effectively bypassing the need to parse an XML document in a browser to get to the actual data. JSON uses a stripped-down syntax compliant with the native JavaScript interpreter provided on all browsers. Access and navigation to JSON data is done through the same standard JavaScript notation used to access string, array or hashtable values in a typical JavaScript application" - Regina Lynch

So if you do like JSON, chances are that you would like the YAML.

References and must reads: