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.