What is the difference between JSON and XML - and which 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 mark up 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. You can use different datatypes in xml using a schema.

JavaScript Object Notation also known as JSON is an object notation used for javascript. The format has been widely adopted on the internet 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 many more years to become popular (or replace XML on the web). in JSON there are several datatypes that you can use.

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 is JSON's strengths are - 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 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 didn't understand it at first. Some legacy systems do not handle JSON at all - here 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. Again I have not used these. The above list really have 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 can do. If you work with Legacy systems you probably 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 API's 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?

My preference as you might have figured out by now - is JSON. Am I wrong in my bias? please let me know in the comments :)

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