- JSON is more compact and can be easily loaded in JavaScript.
- XML is stricter and has support for schemas and namespaces.
On the face of it JSON seems superior in every way - it's flexible, more compact and in many cases easier to use (especially when working with JavaScript), however it lacks some key features, in particular:
Schema support
I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.
This is crucial when passing data between separate systems, where a deviation from the expected format might mean that the data cannot be processed (or worse, is processed incorrectly).
Namespace support
I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.
An example of this in action is the SOAP protocol - namespaces allow for the separation of the SOAP "Envelope", or "Wrapper" data which is passed alongside the serialised application data. This allows web frameworks process and handle the SOAP Envelope and then pass the body / payload data onto the application.
JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required, however it's flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems, or storing data that will be read by 3rd parties.
Perhaps in time these sorts of features will appear in JSON, but for now XML is the dominant format for things like web services and file formats.
Answer from Justin on Stack OverflowXML and JSON -- Advantages and Disadvantages? - Stack Overflow
JSON vs XML
Differences between XML and JSON for data exchange - Software Engineering Stack Exchange
JSON vs XML
What is JSON?
What are the primary differences between JSON and XML?
The primary differences between JSON and XML lie in their structure and syntax. JSON is a lightweight data format that uses key-value pairs, making it easy to read and write. In contrast, XML utilizes a more verbose syntax with tags to represent data, which can add complexity. While JSON is primarily used for data interchange in web APIs, XML excels in data representation requiring strict validation and better compatibility with legacy systems.
Can I convert data between XML and JSON?
Yes, data can be converted between XML and JSON using various tools and libraries available in most programming languages like xmltodict and dicttoxml in python and xml2js and js2xmlparser in node.js. There are also numerous online converters that can quickly transform XML data to JSON format and vice versa.
Videos
- JSON is more compact and can be easily loaded in JavaScript.
- XML is stricter and has support for schemas and namespaces.
On the face of it JSON seems superior in every way - it's flexible, more compact and in many cases easier to use (especially when working with JavaScript), however it lacks some key features, in particular:
Schema support
I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.
This is crucial when passing data between separate systems, where a deviation from the expected format might mean that the data cannot be processed (or worse, is processed incorrectly).
Namespace support
I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.
An example of this in action is the SOAP protocol - namespaces allow for the separation of the SOAP "Envelope", or "Wrapper" data which is passed alongside the serialised application data. This allows web frameworks process and handle the SOAP Envelope and then pass the body / payload data onto the application.
JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required, however it's flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems, or storing data that will be read by 3rd parties.
Perhaps in time these sorts of features will appear in JSON, but for now XML is the dominant format for things like web services and file formats.
Advantages of JSON
- Smaller message size
- More structural information in the document
- Can easily distinguish between the number
1and the string"1"as numbers, strings (and Booleans) are represented differently in JSON. - Can easily distinguish between single items and collections of size one (using JSON arrays).
- Can easily distinguish between the number
- Easier to represent a null value
- Easily consumed by JavaScript
Advantages of XML
- Namespaces allow for sharing of standard structures
- Better representation for inheritance
- Standard ways of expressing the structure of the document: XML schema, DTD, etc
- Parsing standards: DOM, SAX, StAX
- Standards for querying: XQuery and XPath
- Standards for transforming a document: XSLT
Draw
- Human Readable
- Easy to parse
Is there any significant difference between the 2 (other then that one can display the data), and should you use the 1 over the other in certain situations? And if so, what are those situations?
Also, I've read that XML is more secure, but what does that mean?
XML and JSON are both capable of transmitting the same data, but which is better depends mostly on what you want to do with it.
This does touch on existing tooling, but you're not likely to be hand rolling parsers for either, so it is relevant.
XML
- Has better tooling for verifying schema.
- Has built in support for namespaces.
- Can be more easily restructured into HTML.
- Can be queried with XPath, which is really exceptional query language - think SQL for XML. I haven't really seen an equivalent for JSON - but I'd love to hear about it if I'm wrong on this one.
JSON
- Has significantly lower ceremony (and, at least in my experience, better pretty printers), so when a human has to look at it, it can be easier to read.
- Is, unsurprisingly, the best format when one end of the transfer is written in JavaScript.
- Has less legacy, so the chance of being forced to accept malformed JSON is lower. There is also no cultural expectation that you should have to accept malformed input.
Because one is capable of storing anything that can be stored in the other, usability and tooling is about the only criteria that is useful for comparing what it's like to use them.
If all that were wiped away, I'd probably use neither, and transmit the data as chunks of Lisp.
It's lower ceremony than JSON (barely), much easier to transform than XML, and easier to write a parser for than either (which I'd have to do if all the tooling were gone).
XML is an eXtensible Markup Language. That's important to understand: It's designed to be essentially like HTML--a document markup language--but a bit more formalized. Problem is, document markup isn't what anyone uses it for. People abuse it for data storage and data exchange, which is a really bad idea because data storage isn't anything like document markup.
A typical HTML document has a pretty high content-to-markup ratio. It's quite possible to write entire pages without needing to insert a single tag beyond the occasional <p>, for example. But in data storage, every data element needs to be defined in some formal way. With XML, that turns everything into a mess of tag soup very quickly. For example:
Some people, when presented with a problem, think “I know, I’ll use XML.”
<Problem:Worsening> <Problem:TimeDescription>Now</Problem:TimeDescription> <Problem:Posessive>they have</Problem:Posessive> <Problem:Quantity>many, many</Problem:Quantity> <Problem:WorseningDescription>more problems</Problem:WorseningDescription> </Problem:Worsening>
We see that the content can very easily end up completely drowned out by all of the tags. (This is often true even when the XML has been pretty-printed.)
Also, XML requires that every <tag> must be closed by a corresponding </tag>. It also requires that tags be closed in strict LIFO (ie. Stack) order. This is redundant: if the only tag that's valid to close is the last one that hasn't been closed yet, every closing tag could theoretically be a simple </> with no loss of semantic information. (It would be harder to follow if you didn't pretty-print it, but who wants to try to read non-pretty-printed XML anyway?) This makes XML files larger and bulkier than they need to be.
The XML DOM model is also incredibly complex. Just for starters, you have two distinct ways to represent sub-data on a node: nodes between the opening tag and its closing tag, and Attributes inside the opening tag. Again, this is an idea that makes a lot of sense in document markup (<a href="http://example.com">) but a lot less for data storage. Then you've got all sorts of additional complexities like namespaces, schemas and validation, XSLT, and so on...
The principal difference between XML and JSON is that JSON is just a data storage format. It's optimized for expressing serialized data, not for document markup, and so it does a much better job of expressing serialized data. Douglas Crockford calls it "the fat-free alternative to XML."
The really interesting thing is, it manages to do this without actually losing any power. You want schemas and validation? Use JSON Schema. Queries? There's JSONPath for that. (And if, on the other hand, you don't care about those things, you don't need any code for it bloating up your program.) You want comments and namespaces, or even attributes? {"namespace": "MyNamespace", "attributes": {"attrib1": "value1", "attrib2": "value2"}, "data": "Insert content here", "comment": "It's that simple"}
To answer the original question, there's no data that can be represented in either system that the other can't also do. It's just a lot smaller and cleaner in JSON, almost every time, because JSON was specifically designed for data storage and XML wasn't. It's a simple matter of "use the right tool for the job."