Before comparing JSON and XML formats, let’s start with a brief history lesson about the former data formats (more precisely, the ancestors of XML):
* Generalized Markup Language (GML) was developed in the early 1970 by IBM, and is a set of macros that implemented markup tags for SCRIPT.
* Standard Generalized Markup Language (SGML) was developed based on GML as a markup language used for documents and has become an ISO standard in 1986. Later, in 1998, SGML was reworked into the successful XML.
On the other hand, JSON is a newer data format which was popularized by Douglas Crockford in 2001. The data format started being used mainstream (by Yahoo and Google) in 2005 – 2006.
General facts
Extensible Markup Language (XML) is a markup language that encodes documents in a format that is human and machine readable. One of its goals was the usability over the Internet, and nowadays it is widely used in a variety of applications, web services, web sites etc.
JavaScript Object Notation (JSON) is a lightweight format that transmits data objects as a collection of name / value pairs. It was developed as an alternative to XML and it is now supported by all modern programming languages.
From a syntax point of view, the differences are obvious – please see below the following code snippets:
JSON
Code snippet:
{
"companyId":11
"name": "My Company",
"address":{
"streetAddress": "21 2nd Street",
"city": "Bucharest",
"country": "RO",
},
"phoneNumber": [
{
"type": "home",
"number": "021 555-4567"
},
{
"type": "fax",
"number": "021 555-7890"
}
],
}
XML
Code snippet:
<company>
<companyId>11</companyId>
<name>MyCompany</name>
<address>
<streetAddress>21 2nd Street</streetAddress>
<city>Bucharest</city>
</address>
<phoneNumbers>
<phoneNumber type="home">021 555-4567</phoneNumber>
<phoneNumber type="fax">021 555-7890</phoneNumber>
</phoneNumbers>
</company>
What are the advantages of each data format?
As you can see in the above examples, thanks to its indentation and white spaces (when properly formatted), JSON is easier to read than XML. Also, because it doesn’t use a full markup structure, it is more compact, so it occupies less space (even though this is no longer a major issue nowadays).
Another advantage of JSON is that it can contain more structural information. For example: it is easier to express different data formats such as: numbers (11), strings (“11”), NULL values and even arrays.
On the other hand, XML is still widely used by developers. Being older on the “data format market” brings many advantages, such as: more documentation and more tools to work with it. One more advantage that XML has over JSON is the XML schema. This represents the description of the XML document and is often used for data validation.
JSON or XML?
In the end, the choice between JSON and XML is subjective. If you are willing to learn about JSON and the libraries associated with it, then you should probably do it because JSON is gaining more and more popularity and usability. On the other hand, if you are a bit more conservative, then you should stick with XML. The markup language proved its worth over the years and it won’t be fully replaced in the near future.