Why is JSON more lightweight than XML?

2020-06-01 01:27发布

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?

标签: xml json
7条回答
Ridiculous、
2楼-- · 2020-06-01 01:50

There are several factors at play here:

(a) the JSON data model is simpler; it has fewer different kinds of object and they have fewer properties.

(b) the JSON data model is closer to the data models available in common programming languages such as Javascript, so less data conversion is needed when using these languages

(c) the serialized syntax of JSON has less redundancy (is less verbose) than the syntax of XML.

Of course, these differences are because JSON was designed for a different purpose than XML.

查看更多
Juvenile、少年°
3楼-- · 2020-06-01 01:50

Why should we forget security aspect ? While transferring data over network, XML is more secure as we can prepare XML envelope and make SOAP request.

查看更多
姐就是有狂的资本
4楼-- · 2020-06-01 01:54

Simplicity

XML is simpler than SGML, but JSON is much simpler than XML. JSON has a much smaller grammar and maps more directly onto the data structures used in modern programming languages.

Extensibility

JSON is not extensible because it does not need to be. JSON is not a document markup language, so it is not necessary to define new tags or attributes to represent data in it.

Interoperability

JSON has the same interoperability potential as XML.

Openness

JSON is at least as open as XML, perhaps more so because it is not in the center of corporate/political standardization struggles.

Content link

查看更多
Explosion°爆炸
5楼-- · 2020-06-01 01:57

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.

查看更多
▲ chillily
6楼-- · 2020-06-01 01:57

JSON notation is terser. XML repeats the node type in the closing tag whereas JSON just infers closure scope using a stack. XML supports namespaces, as far as I know JSON doesn't. XML schemas typically include type information but there is no instrinsic reason you couldn't do this with JSON. It should be noted that the rise of JSON is due in no small part to the fact that it is functionally equivalent to XML in all the respects that are actually used. Over the wire the difference disappears as soon as LZW compression comes into play because XML yields well to dictionary compression.

查看更多
放荡不羁爱自由
7楼-- · 2020-06-01 02:03

The data model is different. Apart from the lack of markup, which you noticed yourself, JSON also doesn't have all the other XML features, such as:

  • Processing instructions
  • Comments
  • Attributes

Hence, not only the data itself is more lightweight, but also any library implementing the data model. This makes rendering, parsing, processing JSON with non-JavaScript languages a lot faster. For JavaScript, this is a no-brainer, as JSON is actual JavaScript code, and thus much more "lightweight" for a JavaScript parser to process

查看更多
登录 后发表回答