Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is JSON more lightweight than XML?

Tags:

json

xml

I was finding the difference between JSON and XML. As, both are for data interchange between systems, but there is a big difference between JSON and XML that JSON is more lightweight than XML. But I am unable to find the actual reason that why the JSON is light-weight. What makes JSON light-weight??

One answer I found is that in JSON there is not a lot of extra xml markup. What does it actual mean. Are there some more reasons that describes why JSON is lightweight?

like image 571
Arun Kumar Avatar asked Sep 10 '12 06:09

Arun Kumar


1 Answers

Have you looked at examples of JSON and XML documents?

While both JSON and XML are used to represent arbitrary trees of data, XML is far more verbose. Every element in the tree has a name (the element type name), and the element must be enclosed in a matching pair of tags. By contrast, JSON expresses trees in a "nested array" type of notation akin to that of Javascript (in fact, a JSON document can literally be parsed as Javascript to result in the corresponding array).

Take a look at the differences.

XML is of course semantically richer, since elements can be decorated with an arbitrary number of attributes, and elements may contain mixed content of free text flow and further, nested elements. For example, the following snippet would be tedious to express as JSON:

<paragraph>
   This is an entire paragraph of <emph>text</emph>. It is interspersed with
   further, nested <emph>XML elements</emph>, as well as the occasional
   <link href="http://stackoverflow.com/a/12346394/596781">hyperlink</link>.
</paragraph>

On the other hand, data-like documents can be much simpler in JSON. Imagine this:

<properties>
    <property type="int" name="ID">123</property>
    <property type="date" name="birthday">19700101</property>
    <references>
        <url>http://stackoverflow.com/a/12346394/596781</url>
        <doi>something.made.up</doi>
    </references>
</properties>

This becomes a very compact JSON array.

A secondary consideration is the amount of toolsets that surround both formats. While JSON tools are mainly about parsing JSON data, the W3C has been developing a very large set of adherent technologies to manipulate XML in a systematic fashion, such as XSLT and XPATH.

In a nutshell, and as a very personal opinion, I'd say that XML is about the document and JSON is about data. XML will feel more natural and useful for large, structured documents, while JSON is often the simpler and less verbose format for transmitting data sets.

like image 65
Kerrek SB Avatar answered Oct 08 '22 06:10

Kerrek SB