In the late 90s, when standards were proprietary and communication protocols very complicated, one open data format appeared, XML. It born with the purpose of storing and defining documents and data through the optional use of a schema. Years later, the JSON (JavaScript Object Notation) showed up, from a programming language, with the exact opposite, a serialization format with very simple requirements. So… JSON vs XML ?

Resources

  • Extensible Markup Language (XML) link
  • JavaScript Object Notation (JSON) link

Why JSON is better than XML

As it is explained above, XML (extensible markup language) was the only choice for open data interchange. But over the years JSON appeared. Therefore JSON has become a popular alternative to XML for multiple reasons. A couple obvious ones are:

  • Less verbose – XML uses tags and more words than necessary
  • JSON is faster – Parsing XML software is slow and unwieldy. Many of these DOM manipulation libraries can lead to your applications using large amounts of memory due to the verbosity and cost of parsing large XML files.

JSON Example (see JSON Syntax page)

{"cities":[
  { "name":"Zaragoza", "region":"Aragon" },
  { "name":"Barcelona", "region":"Catalunya" },
  { "name":"Madrid", "region":"Madrid" }
]}

XML Example

<cities>
  <city>
    <name>Zaragoza</name> <region>Aragon</region>
  </city>
  <city>
    <name>Barcelona</name> <region>Catalunya</region>
  </city>
  <city>
    <name>Madrid</name> <region>Madrid</region>
  </city>
</cities>

Looks easy to compare, right? JSON is shorter, is easier to understand since it’s less “cryptic”, and is also perfectly parseable in JavaScript. Then… JSON vs XML? (keep reading for the answer).

XML and JSON can play nice together

To answer the question JSON vs XML, we need to understand that JSON it is just a data format, but XML is not a data format. It is a language. A very powerful one. If you need to understand more about this, I recommend you to read XML in a Nutshell, Third Edition by Elliotte Rusty Harold and W. Scott Means.

XML has features like XPath, Attributes, Namespaces, XML Schema, or XSL… and a lot of languages, standards and applications developed around XML. JSON was not designed to have such features. Therefore, to conclude, if you don’t need this additional functionality, use JSON (see Ajax for example!), and if you need these features, use XML 🙂