20

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?

4
  • 1
    A complete answer considers all three of the top answers you received. If you are trying to choose between XML and JSON, it would be meet to ask yourself what purpose it will serve. If you want compact serialisation for transmission or mass storage, JSON is the go. If you want to do document manipluation, XML is, as Kerrek notes, far better supported by adherent technologies. If you are using the .NET framework this is less important because LINQ can query/transform practically anything.
    – Peter Wone
    Sep 14, 2012 at 0:35
  • 1
    @PeterWone: There is nothing like "top three answers" on StackOverflow, because the order varies in time according to casted votes. To refer to specific answers, please name the author or give the permalink. Specifically, at the time of this writing, we have one answer with 21 votes and three answers with 5 votes each. So which ones are the "top three"? :)
    – KPM
    May 9, 2014 at 13:16
  • @KPM - yes, and that's exactly what I meant: the three top-scoring answers at the time of the comment. I did name one of the authors, and my comment was capable of standing on its own; I mention those answers purely as a courtesy to the authors. Anyway, here we are years later and they're still the top three answers as well as the three top answers.
    – Peter Wone
    May 10, 2014 at 1:07
  • I was just trying to be helpful. I found that there were three answers ranking #2, which would make it difficult to determine which of these you would include in the top three. I didn't see that one of them was from you, which removes the ambiguity as I assume you weren't counting your own.
    – KPM
    May 10, 2014 at 13:04

7 Answers 7

30

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.

2
  • 1
    With XML you would declare data types in your schema and not as part of your data. a better example of XML would be <user ID="123" birthday="19700101"><references url="..." doi="..." /></user> Jul 12, 2016 at 20:41
  • 1
    Many of the XML samples on the link provide are invalid and not really a fair comparison to the JSON as they intentionally made bloated XML Jul 12, 2016 at 20:43
8

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

8

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

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

3

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.

3

It's just smaller by a linear factor. That's not really smaller. Once compressed, both formats take about the same space.

Remember that programs that already have to use XML aren't going to get simpler by also allowing JSON, they'll get more complex. Extra standards only complicate things unless you get rid of the old standards, and XML isn't going to disappear soon.

XML was designed around the concept of a document. JSON was more about simple data structures. JSON cares less about standards, i.e. it has no schemas, so portability may be more difficult than for someone skilled in using XML. Usually when I have to use some ReSTful service, I'm told to 'read the code' or 'look at the JSON data itself' in order to understand it. I.e. implicit (and quite often, changing) data formats.

Both formats suffer from the problem that they can, in their natural form, only represent trees, not graphs of data. I don't like either format. They both seem half-finished. Object serialisation solves more problems.

0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.