views:

265

answers:

6

what is the difference between json and xml

A: 

They're two different ways of representing data, but they're pretty dissimilar. The wikipedia pages for JSON and XML give some examples of each, and there's a comparison paragraph

Michael Mrozek
A: 

They are two formats of representation of information. While JSON was designed to be more compact, XML was design to be more readable.

Mendy
Yes, obviously two different representations. But I don't think XML is considered more readable of the two. JSON is usually bit more compact but I don't think that was an important design criteria; rather, readability, simplicity, and ease of use (esp. from javascript).More importantly two actually have quite different logical structure: XML is inlined markup format with hierarchic model; and JSON is object notation with object/frame/graph model (although 'graph' may be overstatement given that it has no concept of object identity).
StaxMan
A: 

JSON is JavaScript Object Notation XML is Extensible Markup Language

silent
how amusing :))
silent
You could also have answered: JSON is JSON, XML is XML
Quandary
hahaha, yeah. It's just, simple google search does answer it.
silent
A: 

XML uses a tag structures for presenting items, like <tag>item</tag>, so an XML document is a set of tags nested into each other. And JSON syntax looks like a construction from Javascript language, with all stuff like lists and dictionaries:

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

So if you use JSON it's really simple to use a JSON strings in many script languages, especially Javascript and Python.

Enchantner
At high level yes, but your example is not valid JSON: field names must be quoted with double quotes. Same for String values.
StaxMan
Oops, sorry, it's fixed now :)
Enchantner
+10  A: 

They are both data formats for hierarchical data, so while the syntax is quite 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 shown by the example, though. You can for example add attributes to each element, and you can use namespaces to partition elements. There are also standards for defining the format of an XML file, the XPATH language to query XML data, and XSLT for transforming XML into presentation data.

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

While XML was developed as an independent data format, JSON was developed specifically for use with Javascript and AJAX, so the format is exactly the same as a Javascript literal object (that is, it's a subset of the Javascript code, as it for example can't contain expressions to determine values).

Guffa
"the format is exactly the same as a Javascript literal object." - not exactly, JSON texts are a subset of JS object literals.
Daniel Earwicker
@Daniel: Of course it is a subset, you can naturally not write anything that you can in a Javascript, like for example call a function to get a value for a member. Is that why you downvoted the answer?
Guffa
Argh... I actually intended to upvote you, now SO won't let me fix it unless you make an edit. Check "exactly the same as a" to "a subset of the format of a" and I'll fix it ASAP.
Daniel Earwicker
Words fail me...
Martin Bean
Done. *(face palm)*
Daniel Earwicker
+10  A: 

The fundamental difference, which no other answer seems to have mentioned, is that XML is a markup language (as it actually says in its name), whereas JSON is a way of representing objects (as also noted in its name).

A markup language is a way of adding extra information to free-flowing plain text, e.g

Here is some text.

With XML (using a certain element vocabulary) you can put:

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

This is what makes markup languages so useful for representing documents.

An object notation like JSON is not as flexible. But this is usually a good thing. When you're representing objects, you simply don't need the extra flexibility. To represent the above example in JSON, you'd actually have to solve some problems manually that XML solves for you.

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

It's not as nice as the XML, and the reason is that we're trying to do markup with an object notation. So we have to invent a way to scatter snippets of plain text around our objects, using "content" arrays that can hold a mixture of strings and nested objects.

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

{
    "firstName": "Homer",
    "lastName": "Simpson",
    "relatives": [ "Grandpa", "Marge", "The Boy", "Lisa", "I think that's 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's all of them</Relative>
    </Relatives>
</Person>

JSON looks more like the data structures we declare in programming languages. Also it has less redundant repetition of names.

But most importantly of all, it has a defined way of distinguishing between a "record" (items unordered, identified by names) and a "list" (items ordered, identified by position). An object notation is practically useless without such a distinction. And XML has no such distinction! In my XML example <Person> is a record and <Relatives> is a list, but they are not identified as such by the syntax.

Instead, XML has "elements" versus "attributes". This looks like the same kind of distinction, but it's not, because attributes can only have string values. They cannot be nested objects. So I couldn't have applied this idea to <Person>, because I shouldn't have to turn <Relatives> into a single string.

By using an external schema, or extra user-defined attributes, you can formalise a distinction between lists and records in XML. The advantage of JSON is that the low-level syntax has that distinction built into it, so it's very succinct and universal. This means that JSON is more "self describing" by default, which is an important goal of both formats.

So JSON should be the first choice for object notation, where XML's sweet spot is document markup.

Unfortunately for XML, we already have HTML as the world's number one rich text markup language. An attempt was made to reformulate HTML in terms of XML, but there isn't much advantage in this.

So XML should (in my opinion) have been a pretty limited niche technology, best suited only for inventing your own rich text markup languages if you don't want to use HTML for some reason. The problem was that in 1998 there was still a lot of hype about the Web, and XML became popular due to its superficial resemblance to HTML. It was a strange design choice to try to apply to hierarchical data a syntax actually designed for convenient markup.

Daniel Earwicker
Two downvotes, no comments! C'est la vie.
Daniel Earwicker
@Daniel: What downvotes? I don't see any. If you are fishing for sympathy votes, you should at least have a reason...
Guffa
Although it's true that XML does not distinguish between lists and named collections in the syntax, you can very easily add the distinction if you want to use XML to represent an object, for example by simply adding an attribute like `isarray="true"`. You can also use a DTD in the XML document to describe it's structure, which offers much more control than JSON can.
Guffa
No, not remotely interested in "sympathy votes".
Daniel Earwicker
@Guffa - that's correct. The distinction is in how self-describing the basic syntax is; JSON has built-in list/record distinction, whereas XML requires additional schema or user-defined annotations to make that distinction. I will clarify that.
Daniel Earwicker
Oh dear, it looks like I mistook my own downvotes for other people's, one of which was also accidental. Not too clever...
Daniel Earwicker
You forgot Maggie
Pranav
@Pranav - I was writing it as if Homer entered the data himself...
Daniel Earwicker
of course...hehe
Pranav
XML is not at all "only for inventing your own rich text markup languages". It can be used for many different kinds of data, and is widely used way beyond the limited field of text markup.
Guffa
@Guffa - that was meant as a "should" rather than an "is", I've clarified it as my opinion of what went wrong.
Daniel Earwicker