JSON vs XML: should I use JSON or XML?

| 7 min read

Both JSON and XML are data interchange formats, and they have the same purpose. JSON stands for “JavaScript Object Notation”, and XML stands for “Extensible Markup Language”.

This article explains the main differences between these two data formats and when to use JSON vs XML.

Convert JSON to XML or XML to JSON

First, a practical solution: you can use the editor below to convert JSON to XML or XML to JSON:

XML
loading...
JSON
loading...

Should I use JSON or XML?

Simply put: use JSON over XML whenever possible.

In most cases, JSON clearly is the better choice over XML. We will discuss why in this article. You may choose XML over JSON though when you are working in an environment that already heavily uses XML and XML tooling, or needs to integrate with other XML based applications or libraries, or if you have very specific constraints.

You can read on to learn more about how the two data formats differ.

How does JSON differ from XML?

In short: JSON is a much simpler format.

Here is a small example to get a feel of the difference. The following example shows a an object with user information in JSON:

{
  "name": "Joe",
  "nickname": "Big Joe",
  "score": 12.4,
  "subscribed": true,
  "friends": ["Sarah", "Robert", "Paul"]
}

Here is the same data in XML:

<?xml version = "1.0" encoding = "UTF-8" ?>
<!-- comment: modeled using elements -->
<User>
    <Name>Joe</Name>
    <Nickname>Big Joe</Nickname>
    <Score>12.4</Score>
    <Subscribed>true</Subscribed>
    <Friends>
        <Friend>Sarah</Friend>
        <Friend>Robert</Friend>
        <Friend>Paul</Friend>
    </Friends>
</User>

The same data could also be modeled using XML attributes:

<?xml version = "1.0" encoding = "UTF-8" ?>
<!-- comment: modeled using attributes -->
<User name="Joe" nickname="Big Joe" score="12.4" subscribed="true">
    <Friends>
        <Friend name="Sarah" />
        <Friend name="Robert" />
        <Friend name="Paul" />
    </Friends>
</User>

There are no rules about whether to use elements or attributes in XML. One idea is to put “data” in elements, and “metadata” in tags. However, the difference between data and metadata is very subjective.

The most notable differences between JSON and XML are as follows

The pro’s of XML:

  • XML does support comments. The lack of support for comments in JSON is a common one.
  • XML has concepts of namespaces (to help avoid element name conflicts) and metadata (which is data inside tags as opposed to data inside elements).
  • XML can contain a reference to a document schema or include the schema itself using DTD (document type definition).

The pro’s of JSON:

  • JSON stores data as an object with keys and values (a map), or as an array with items. These are important fundamental data structures, and this makes it easy to map a JSON document to a type system in a programming language. With XML this is relatively complex, because XML doesn’t have an explicit array structure: everything is a nested tree.
  • JSON does have data types object, array, string, number, boolean, and null. In XML, everything is a string.
  • JSON is more efficient and performant: it is much less verbose, requiring less bytes to serialize the same data. Serializing, exchanging, and deserializing is faster and requires less data transfer and disk space.
  • JSON is much simpler to parse since it is a simpler format. It is easier to develop tooling for JSON than for XML.
  • JSON is just JSON, whereas XML has two versions: 1.0 and 1.1, making XML more complex to use.

Now, it is important to note that the shortcomings of JSON (lack of comments, namespaces, or embedded schema) are surmountable by using conventions. For example, a common case is to use metadata containing class information (a schema) in a property named "_type" or "@type". Of course, the use of these conventions will be limited in scope because there is no standard.

To learn JSON or XML more in-depth, you can read the articles “What is JSON?” and “XML Tutorial for Beginners”, and there are many good resources available on the web to learn more.

JSON and XML Tooling

Besides differences in the data formats themselves, support and tooling around them is just as important.

Since the JSON and XML data formats are so mature, you’ll find libraries to use them for data serialization and deserialization in practically every programming language, and built-in solutions in JavaScript in the browser. This is the most important.

For both XML and JSON there are excellent ways to validate the structure of a document, but also validate the data against a schema. In case of XML there is built-in validation support in the form of DTD (document type definition). In the case of JSON, you can use for example JSON Schema.

Then, as a developer, you may want to use additional tooling to handle either JSON or XML data, most notably an editor to view and edit data. Most IDE’s support both XML and JSON out of the box, and there are online and offline tools to work with both data types too.

All in all, both XML and JSON come with all the tooling you need, and there is no need to choose one or the other over a lack of proper tooling.

A bit of history about XML and JSON

XML became standardized 1998. It originated from SGML, which is a standard “for defining generalized markup languages for documents”. Its goal was to create a text based data format to exchange arbitrary data on the web. It was designed in the time that the web mostly consisted of static HTML pages. It was meant to be the one and only data format for all data exchange on the web, and there has been quite some hype around it.

JSON was discovered in 2000/2001 by Douglas Crockford as a subset of JavaScript, which was created in 1995 by Brendan Eich for the Netscape 2 browser. Douglas needed a simple solution to exchange data with the browser. XML was way too complex, and then he came up with the idea to send the data as a JavaScript object, utilizing the JavaScript parser that was already there in the browser to parse the data. This was a very fast and simple solution, and required no additional code at all on the client side. Douglas defined a small subset of JavaScript needed to define any data structure: object, array, string, number, boolean, and null. Douglas formalized this and called it JSON, which stands for JavaScript Object Notation. Douglas explains the history of JSON in detail on YouTube in “Douglas Crockford: the JSON Saga”.

It was an uphill battle for JSON: the big companies and consultants had already embraced XML, and many people simply could not believe that data exchange could be as simple as JSON.

Nowadays, JSON has become the de facto standard to exchange data on the web, and it has found much more use, like in databases. Is XML still being used? Yes, mostly in legacy systems. But it has its niches and probably always will. The XML dream of one standard that rules them all did not play out though. Today, we have plenty of different formats for different use cases. And this makes sense: document formats, data formats, configuration formats are simply quite far apart and have different requirements. When choosing a format, it is best to look for the best tool for the job. JSON too may be replaced some day when our needs in the future change.

Conclusion

Both JSON and XML are data formats, largely having the same purpose. Both formats are mature and well-supported. JSON is a much simpler format and is in most cases is the better choice over XML.