This is a mobile version, full one is here.

Yegor Bugayenko
16 November 2015

Stop Comparing JSON and XML

JSON or XML? Which one is better? Which one is faster? Which one should I use in my next project? Stop it! These things are not comparable. It’s similar to comparing a bicycle and an AMG S65. Seriously, which one is better? They both can take you from home to the office, right? In some cases, a bicycle will do it better. But does that mean they can be compared to each other? The same applies here with JSON and XML. They are very different things with their own areas of applicability.

Here is how a simple JSON piece of data may look (140 characters):

{
  "id": 123,
  "title": "Object Thinking",
  "author": "David West",
  "published": {
    "by": "Microsoft Press",
    "year": 2004
  }
}

A similar document would look like this in XML (167 characters):

<?xml version="1.0"?>
<book id="123">
  <title>Object Thinking</title>
  <author>David West</author>
  <published>
    <by>Microsoft Press</by>
    <year>2004</year>
  </published>
</book>

Looks easy to compare, right? The first example is a bit shorter, is easier to understand since it’s less “cryptic,” and is also perfectly parseable in JavaScript. That’s it, then; let’s use JSON and manifest the death of XML! Who needs this heavyweight 15-year-old XML in the first place?

Well, I need it, and I love it. Let me explain why.

And don’t get me wrong; I’m not against JSON. Not at all. It’s a good data format. But it’s just a data format. We’re using it temporarily to transfer a piece of data from point A to point B. Indeed, it is shorter than XML and more readable. That’s it.

XML is not a data format; it is a language. A very powerful one. Let me show you what it’s capable of. Let me basically explain why I love it. And I would strongly recommend you read XML in a Nutshell, Third Edition by Elliotte Rusty Harold and W. Scott Means.

I believe there are four features XML has that seriously set it apart from JSON or any other simple data format, like YAML for example.

This is not a full list, but these four features really mean a lot to me. They give my document the ability to be “self-sufficient.” It can validate itself (XML Schema), it knows how to modify itself (XSL), and it gives me very convenient access to anything inside it (XPath).

There are many more languages, standards, and applications developed around XML, including XForms, SVG, MathML, RDF, OWL, WSDL, etc. But you are less likely to use them in a mainstream project, as they are rather “niche.”

JSON was not designed to have such features, even though some of them are now trying to find their places in the JSON world, including JSONPath for querying, some tools for transformations, and json-schema for validation. But they are just weak parodies compared to what XML offers, and I don’t think they have any future. Or let’s put it this way: I wish they would disappear sooner or later. They just turn a good, simple format into something clumsy.

Thus, to conclude, JSON is a simple data format with no additional functionality. Its best-use case is AJAX. In all other cases, I strongly recommend you use XML.