What is the difference between json and xml

What is the difference between JSON and XML?

+68
json comparison xml
Apr 12 2018-10-12T00:
source share
6 answers

The fundamental difference, which was not mentioned in any other answer, is that XML is a markup language (as its name actually says), while JSON is a way of representing objects (as also noted in its name).

Markup language is a way of adding additional information to freely distributed plain text, for example

Here is some text. 

With XML (using a dictionary of a specific element) you can put:

 <Document> <Paragraph Align="Center"> Here <Bold>is</Bold> some text. </Paragraph> </Document> 

This is what makes markup languages โ€‹โ€‹useful for presenting documents.

JSON-like object notation is not so flexible. But this is usually good. When you present objects, you simply don't need extra flexibility. To present the above example in JSON, you really need to solve some of the problems manually that XML solves for you.

 { "Paragraphs": [ { "align": "center", "content": [ "Here ", { "style" : "bold", "content": [ "is" ] }, " some text." ] } ] } 

This is not as good as XML, and the reason is that we are trying to make markup with musical notation of the object. Therefore, we need to invent a way to disperse fragments of plain text around our objects using "content" arrays, which can contain a mixture of strings and nested objects.

On the other hand, if you have a typical hierarchy of objects and want to represent them in a stream, JSON is better suited for this task than HTML.

 { "firstName": "Homer", "lastName": "Simpson", "relatives": [ "Grandpa", "Marge", "The Boy", "Lisa", "I think that all of them" ] } 

Here's the logically equivalent XML:

 <Person> <FirstName>Homer</FirstName> <LastName>Simpsons</LastName> <Relatives> <Relative>Grandpa</Relative> <Relative>Marge</Relative> <Relative>The Boy</Relative> <Relative>Lisa</Relative> <Relative>I think that all of them</Relative> </Relatives> </Person> 

JSON is more like data structures that we declare in programming languages. It also has fewer redundant name repetitions.

But most importantly, he has a certain way of distinguishing between a "record" (elements are disordered, identified by names) and a "list" (ordered by positions, positions). Object notation is almost useless without such a difference. And XML doesn't have that difference! In my example, XML <Person> has a record, and <Relatives> is a list, but they are not identified as such by syntax.

Instead, XML has โ€œelementsโ€ and โ€œattributesโ€. This seems like the same difference, but it is not, because attributes can only have string values. They cannot be nested objects. Therefore, I could not apply this idea to <Person> , because I would not have to turn <Relatives> into one line.

Using an external schema or optional user attributes, you can formalize the difference between lists and entries in XML. The advantage of JSON is that low-level syntax has this distinction built into it, so it is very concise and universal. This means that by default, JSON is more "self-describing", which is an important goal of both formats.

So, JSON should be the first choice for designating an object where the sweetness of XML is the markup of the document.

Unfortunately, for XML, we already have HTML as the number one multilingual markup language in the world. An attempt has been made to reformulate HTML in terms of XML, but this is not a big advantage.

Thus, XML should (in my opinion) be a rather limited niche technology, best suited only to invent your own rich textual markup languages, if for some reason you do not want to use HTML. The problem was that there was still a lot of hype on the Internet in 1998, and XML became popular due to its superficial resemblance to HTML. It was an odd design choice to try to apply syntax to hierarchical data that was actually designed for convenient markup.

+144
Apr 12 '10 at 7:25
source share

Both of them are data formats for hierarchical data, therefore, although the syntax is very different, the structure is similar. Example:

JSON:

 { "persons": [ { "name": "Ford Prefect", "gender": "male" }, { "name": "Arthur Dent", "gender": "male" }, { "name": "Tricia McMillan", "gender": "female" } ] } 

XML:

 <persons> <person> <name>Ford Prefect</name> <gender>male</gender> </person> <person> <name>Arthur Dent</name> <gender>male</gender> </person> <person> <name>Tricia McMillan</name> <gender>female</gender> </person> </persons> 

The XML format is more advanced than the example. For example, you can add attributes to each element, and you can use namespaces to separate elements. There are also standards for defining an XML file format, XPATH for querying XML data, and XSLT for converting XML to presentation data.

The XML format has existed for some time, so a lot of software has been developed for it. The JSON format is quite new, so its support is much less.

Although XML was designed as an independent data format, JSON was designed specifically for use with Javascript and AJAX, so the format is exactly the same as a literal Javascript object (i.e., it is a subset of Javascript code, because for example, it cannot contain expressions to define values).

+27
Apr 12 '10 at 7:00
source share

The difference between XML and JSON is that XML is a meta-language / markup language, and JSON is an easy data exchange. Thus, the XML syntax is designed specifically to have no internal semantics. Specific element names mean nothing until a specific processing application processes them in a specific way. In contrast, JSON syntax has special semantics built into things between {} - this is an object, things between [] - this is an array, etc.

Therefore, the JSON parser knows exactly what each JSON document means. The XML parser only knows how to separate markup from data. To understand the meaning of an XML document, you must write additional code.

To illustrate this, let me take an example with Guffa:

 { "persons": [ { "name": "Ford Prefect", "gender": "male" }, { "name": "Arthur Dent", "gender": "male" }, { "name": "Tricia McMillan", "gender": "female" } ] } 

The XML equivalent that it gives is not exactly the same, because although the JSON example is semantically complete, XML must be interpreted in a certain way in order to have the same effect. Essentially, JSON is an example of using an established markup language whose semantics are already known, while an XML example creates a new markup language without any predefined semantics.

The best XML equivalent would be to define a (fictitious) XJSON language with the same semantics as JSON, but using XML syntax. It might look something like this:

 <xjson> <object> <name>persons</name> <value> <array> <object> <value>Ford Prefect</value> <gender>male</gender> </object> <object> <value>Arthur Dent</value> <gender>male</gender> </object> <object> <value>Tricia McMillan</value> <gender>female</gender> </object> </array> </value> </object> </xjson> 

After you wrote the XJSON processor, it could do the same as the JSON processor for all the data types that JSON can represent, and you could translate data between JSON and XJSON without loss.

So complaining about XML not having the same semantics as JSON means missing the point. XML syntax does not require semantics. The bottom line is to provide a basic syntax that you can use to create markup languages โ€‹โ€‹with any semantics you want. This makes XML great for creating custom data and document formats, because you don't need to create parsers for them, you just need to write a processor for them.

But the downside of XML is that the syntax is verbose. For any markup language you want to create, you can come up with a much more concise syntax that expresses the specific semantics of your particular language. Thus, the JSON syntax is much more compact than my hypothetical XJSON above.

If it follows that for truly widely used data formats, the extra time required to create a unique syntax and write a parser for this syntax is compensated by the greater conciseness and more intuitive syntax of the user markup language. It also follows that it often makes sense to use JSON with its installed semantics, rather than creating many XML markup languages, for which you then need to implement semantics.

It also follows that it makes sense to prototype certain types of languages โ€‹โ€‹and protocols in XML, but as soon as the language or protocol becomes generally accepted, consider creating a more compact and expressive user syntax.

It is interesting to note that SGML recognized this and provided a mechanism for specifying abbreviated markup for an SGML document. So you could write SGML DTD for JSON syntax that would allow the JSON document to be read by SGML parser. XML has removed this feature, which means that today, if you want a more compact syntax for a particular markup language, you must leave XML behind, as JSON does.

+15
Nov 03 2018-11-11T00:
source share

These are two different ways of presenting data, but they are quite different. The wikipedia page for JSON and XML give a few examples of each, and there is a paragraph comparison

+4
Apr 12
source share

These are two formats for presenting information. While JSON was designed to be more compact, XML was design to be more readable.

+3
Apr 12
source share

XML uses tag structures to represent elements, for example, <tag>item</tag> so an XML document is a collection of tags nested together. And the JSON syntax is like a Javascript construct with all things like lists and dictionaries:

 { 'attrib' : 'value', 'array' : [1, 2, 3] } 

So, if you use JSON, it is very simple to use JSON strings in many script languages, especially Javascript and Python.

+2
Apr 12 '10 at 6:45
source share



All Articles