JSON vs XML

A comparison of JSON and XML for data interchange on the web.

Many developers compare JSON to XML when building applications that share data across disparate systems, especially across the web. This often turns into a "JSON vs XML" argument as each side defends their preferred format.

It turns out that JSON and XML have slightly different purposes, and so it's not always fair (or relevant) to say that one is "better" than the other. It depends on the purpose.

First, let's look at an example of a JSON and XML document containing the same data.

JSON Example

Here is some data contained in a JSON document.

XML Example

Here's the same data contained in an XML document.

Learnability

Being tag based, XML could be slightly easier to learn for someone who's used to markup languages, such as HTML or ColdFusion.

However, a similar statement could be said in favor of JSON for anyone comfortable in JavaScript or C-based programming.

Also the simplicity of both languages probably makes this a non-issue. Anyone having difficulty understanding either the XML syntax or the JSON syntax is unlikely to be comfortable with any programming or markup language.

However, there's a lot less to learn with JSON — simplicity is one of JSON's strengths. XML on the other hand, has rules around case-sensitivity, closing tags, attributes, etc, so XML would probably take longer for most people to learn. Plus, once you move away from simple data interchange, there's a lot more to XML than meets the eye.

File Size

XML requires closing tags (unless it's a self-closing tag), and will most likely result in a larger file size. This could easily impact on the time required to transmit the file (especially if it's a large file, and if it's being transmitted across the Internet).

It could also be argued that adding closing tags means more typing for developers. While this could be true in some cases, most good developers use software that automatically closes tags for them. So this would be a non-issue in these cases.

Metadata

One big advantage that XML has over JSON is metadata. In XML you can use attributes to store metadata. In XML attributes can be placed inside an element to provide more information about that element.

For example, you could do something like this:

While it would be possible to provide metadata in JSON, for example by turning an entity into an object, then adding the metadata as items in the object, this is not as clean as the XML method.

Readability

Browsers tend to display XML in a way that could make it easier for a human to read. The reader can immediately see the heirachical structure, and they can expand and collapse any element within the tree.

Performance

Research often finds that JSON is faster and uses fewer resources than XML. For example, Comparison of JSON and XML Data Interchange Formats: A Case Study [PDF] from the Montana State University.

Also, here's an interesting article comparing performance between XML, JSON, CSV, and Protocol Buffers using various parsers. JSON appeared to have the edge over XML in various performance tests.

Purpose

JSON and XML serve two different purposes.

JSON is a data interchange format. Its purpose is to faciliate structured data interchange. It achieves this by directly representing objects, arrays, numbers, strings, and booleans that are often present in the source environment and the destination.

XML on the other hand, is a markup language. XML's purpose is document markup. Although XML can be easy to learn for the purpose of data interchange, there's a lot more to learn if you want to become proficient in using XML as a markup language. For example, you'll need to learn things like, how to create a document type definition, how to use XSLT to transform XML documents into another form, and how to query XML documents using XPath.

Conclusion

In conclusion, both JSON and XML are suitable for data interchange. Whichever you use will depend on the situation.

JSON certainly appears to have the edge in many use cases, but that's not the whole story. You might encounter many cases where XML is the most suitable technology for the job.

However, JSON's popularity is increasing, and it certainly appears to be replacing XML as the format of choice for data interchange on the web.

Also, these aren't the only data interchange formats. For example, there are other formats, such as Protocol Buffers, and depending on your situation, you might even find CSV the best format for the job.