Summarizing the JSON and XML data interchange format debate
One of my coworkers recently expressed some displeasure with debates that are fundamentally fueled by industry experts. The discussions are typically inflated with bias, academic tripe, and are of little use to the every day working (wo)man. I could not disagree. In the spirit of redemption, I present to you a more humble view of JSON and XML.
The description data interchange formats implies a mystical and unknowable set of technologies, but quite the opposite is true. If you are using XML in AJAX, or JavaScript object literal notation, then you know data interchange formats quite well. At their core these technologies allow one language to pass data along to another language in a predetermined format.
XML is still the most widely used and recognized data interchange format, while JSON is steadily gaining favor among Web aficionados. JSON is based upon ECMAScript, which is the father of JavaScript, so the syntax is similar. The following snippet is an example of JSON, which could be inline, be contained in an external .js file, or also be received by requesting it from a third-party like Del.icio.us or Yahoo! Pipes.
{
"People" : [{
"First Name": "John",
"Last Name": "Doe",
"Age": 25,
"Occupation": "Web Developer"
},{
"First Name": "Jane",
"Last Name": "Doe",
"Age": 25,
"Occupation": "Database Architect"
}]
}
Compare this to an XML file with a similar structure.
<people>
<person>
<first_name>John</first_name>
<last_name>Doe</last_name>
<age>25</age>
<occupation>Web Developer</occupation>
</person>
<person>
<first_name>Jane</first_name>
<last_name>Doe</last_name>
<age>25</age>
<occupation>Database Architect</occupation>
</person>
</people>
On the surface both appear to represent the information in a format that is clearly defined and human-readable. However, as the amount of data increases significantly, personal preferences can change. Some developers consider XML to be too cumbersome because it is necessary to provide open and close tags. On the other hand, JSON is just a series of key/value pairs, but employs the use of easy to miss braces, brackets and commas.
Primary advantages of JSON
- Making a request across domains does not violate the same domain (origin) security policy enforced when requesting XML using XMLHttpRequest.
- JSON is native to JavaScript, which makes traversing data in the format with JavaScript much faster than traversing XML with JavaScript.
- JSON data can be sent and received in the same “call” to another script.
Primary advantages of XML
- Popular languages like Java, .NET, Ruby on Rails and PHP have built-in libraries that make traversing XML easier. This is usually done through XPath support.
- XML is ingrained in several Web applications for managing software configuration, and it is known and well-documented by developers.
- It is much easier to translate XML to XHTML because the two specifications are so tightly coupled.
JSON myths
- JSON is just for JavaScript, and it does not support interoperability.
This is a fancy way of saying that JSON is limited in how it can communicate with other languages, servers, and technologies. This is not true because JSON has been tested in several production environments, and has been used with a variety of applications. - JSON is just for the Web. With XML you can exchange data on the desktop as well.
Desktop widget platforms like Yahoo! Widgets, and APIs like Adobe AIR use JavaScript as the core development language. As such, JSON gets native support. For that matter, any desktop API that supports JavaScript should support JSON. - JSON is unable to handle text-heavy data structures.
Again, this is a matter of personal preference. There is no limitation on the amount of data JSON is capable of handling.
XML myths
- XML traversing requires a custom wrapper, or XPath support, while JSON can be traversed “out-of-the-box”.
If you want to traverse XML or JSON in any language other than JavaScript, then it will always require a wrapper. Most applications need to convert data from a format into objects that it can understand, like a product or a user object that has unique properties. This will always require additional effort. - XML is the right tool for the job because it can support binary data types like images.
XML can support binary data types, but it requires conversion tools, and the performance of XML containing large quantities of binary data is questionable. Just like JSON, raw XML is plain text. Most developers who support this argument will never use binary data in XML. - XML is just too bloated
Even though the file size of XML will outweigh the file size of JSON with the same data, bloated is a bit of an exaggeration. Most developers have found that the difference in file size is negligible, and is not evidence enough to switch formats.
Conclusion
Any Web developer looking to diversify skillsets will need to understand how to use both formats. If you have yet to use one or the other in a sandbox or production environment, then I suggest you give it a try. Head over to Programmable Web and browse through the selection of APIs — several support JSON, while most require some knowledge of XML. Get a handle on the basics, and a personal preference will evolve over time.
If you have experience using both data interchange formats, and would like to share some primary advantages, then please feel free to leave a comment.
Leave a Comment
Comments are moderated. No profanity. Only <a>...</a>, <blockquote>...</blockquote>, and <code>...</code> are allowed.
Seperate paragraphs by pressing the "Enter" key twice, or press it once to break to a new line.
3 Comments
#01, Oct 03 2007
Bryant Cutler
Actually, JSON is pretty close to the intersection between Javascript’s object literals and Python’s dictionary syntax, so almost all JSON can be traversed natively in Python as well as Javascript.
#02, Oct 03 2007
Brian
Hi Bryant,
Thanks for sharing the tidbit — not being a student of Python I was unaware of that fact.
#03, Oct 18 2007
Marc
it keeps difficult to decide for the “right” data interchange format, thank you for this article anyway. - I would be interested in a comparison of time and resource consumption for the two. Does someone has reliable data about it? - I only found a comparison of php JSON-libs on gggeek.altervista.org/sw/article_20061113.html
which may be interesting for someone else too. - I like JSON on one side and SimpleXML on the other and I would be glad about a stable bi-directional interface between the two converting XML -> JSON -> XML
. JSON or XML, SimpleXML