What is the difference between JSON and XML - and which one to use?

This comes up once in a while. Why use JSON instead of XML? Why choose one over the other? They seem so alike and yet I still find people discussing the perks of the two. Here I will give some points on the differences of the two.

EXtensible Markup Language Also known as XML is a markup language aimed at creating documents. However what it is mostly used for is, storing data in a structured way. Common XML implementations are: RSS, XHTML and SOAP. It is often used to transfer serialized data between systems - which can be read from any programming language. XML was developed in 1996 and you can define and use different data types in xml using a schema.

An example of XML:

<person>
   <name>Peter</name>
   <lastname>Rasmussen</lastname>
</person>

JavaScript Object Notation also known as JSON is an object notation used for javascript. The format has been widely adopted on the internet using the HTTP protocol as the standard for exchanging data. Like XML it is a language independent data format. Many think that JSON is a lot newer than the XML format. However JSON was made back in 1999, making it only 3 years younger than XML. It just took more years to become popular (or replace XML on the web). In JSON there are several data types that you can use.

An example of JSON:

{
   "person" : {
      "name":"Peter",
      "lastname":"Rasmussen"
   }
}

Something that both JSON and XML share is that they are readable by humans as well as machines.

So which one to use?

These are the things that I think are JSON's strengths - over XML:

  • JSON is smaller in size (byte wise)
  • The name of an element is only written once (There is no closing tag). Again this makes it smaller, less is more.
  • JSON has arrays which easily define lists
  • XML can have attributes on it's elements, which sometimes can lead to some questions - should attributes be parsed the same way as elements? When/why use attributes at all?
  • In JSON it is easy to distinguish between numbers and letters. Such as "123" vs 123.

JSON is easily parsed to javascript objects - which is a large factor as to why it is used on the web. Many clients (browsers) have an easier time dealing with JSON than XML. Some say that they can more easily read one over the other. Whether you find JSON or XML to be more readable I really think depends on what you have worked with. I know that the first time I read JSON I did not understand it at all. Some legacy systems do not handle JSON, in this situation there really is no choice. Some say that XML schemas are very powerful. They can be used to validate and check the XML. In recent years JSON has also had the addition of schemas, but I have personally never used these. The above list really has no killer puns as to why use JSON over XML. They are very much alike and this leads me to my conclusion:

In the end I would say that the difference between JSON and XML is very small. I believe you should do whatever is the easiest in your current situation. It often comes down to what you and your systems can do. If you work with Legacy systems you probably have to use XML. If you are working with javascript you are most likely using JSON. It all depends on the situation. When I have been working with APIs I have often made it possible to do both. Many web services implement both XML and JSON, both for the request and response. So why not do both?

Feel free to leave a comment down below!

ps: I also have my own online JSON to XML converter