It’s no secret that, XML and JSON is the de-facto standard data-interchange formats in web development. (If you’re a C# developer, i’m sorry, this article contains no love for YAML)
But what is really the difference between the two, and is there any use cases where one cannot replace the other?
This is the questions i recently faced, did some research and here’s a simplified currated list of my results.
Disclaimer: These results are from personal experience and the opinion of a developer who works with this on a weekly basis, and someone who have been debugging all kinds of issues related to both.
|Supports only UTF-8 encoding.||It doesn’t supports comments.|
|It supports arrays||No namespace support, hence poor extensibility|
|Easier to read and interpres by human beings||Does not provide any display capabilities|
|JSON parsing is safe almost all the time except if JSONP is used (which can lead to Cross-Site Request Forgery “CSRF” attack)|
|It supports comments.||It supports various encodings.|
|Good namespace support, many different extensibility options in Schema||It doesn’t support arrays|
|Provide any display capabilities through stylesheets||Can be trivial to read and interpret|
|XML structures are prone to some attacks as external entity expansion and DTD validation are enabled by default.|
There are good reasons for using JSON, and there are still good reasons for using XML. The data structure you choose really depends on what you are working to accomplish, the audience and the data that will be shared. XML’s strength is extensibility and the avoidance of namespace clashes. It holds any data type and can be used to transport full documents with formatting information included.
JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.
XML is best used when transporting something like a patient chart or text document with markup included.
JSON is purposefully limited and therefore much lighter than XML. I suspect that, most of the time, data can be modeled with hashes and lists comprising simple data types, making JSON the preferred route.
JSON is a better data exchange format. XML is a better document exchange format. Use the right tool for the right job.