XML and JSON -- Advantages and Disadvantages?
Solution 1
- 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.
Solution 2
Advantages of JSON
- Smaller message size
- More structural information in the document
- Can easily distinguish between the number
1
and 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
Solution 3
XML
- Can have a schema that states its format.
- This is of interest to quality control people. You can prove that its format matches what is expected, and therefore you may not have to be quite as fervent as you might otherwise be at checking that a field exists within it every time you want to reference one.
- (Though this pre-supposes that you go out of your way to actually validate the XML against its schema.)
- Bloated; each field name has to be written out twice per field. Ew!
JSON
- Far less bloated, easier to parse and arguably more human readable (if you space it out properly).
- Not quite as powerful: not expressive enough to separate attributes from values.
Solution 4
Advantages of XML
Near ubiquitous support in a wide array of languages and frameworks. More likely than not there's already a tool out there to help your extract information from an XML response.
It can adhere to a concrete schema if so you choose. Once it validates, you can say it's correct and start parsing.
Namespaces allow you to divide the XML.
Advantages of JSON
Lightweight in comparison to XML. Fewer characters = smaller time going through the
internet tubes
.Easier to handle with Javascript if you need something for a web application.
Solution 5
JSON - smaller and can be natively loaded as JavaScript object (speed is a value)
XML - still standard, however older (slower, bigger, but not only JS)
Related videos on Youtube
user541686
Updated on July 09, 2022Comments
-
user541686 almost 2 years
I recently heard about JavaScript Object Notation (JSON), and after looking it up, it seems like it's becoming rather popular as an alternative to the Extensible Markup Language (XML).
I went on this page for more info, but it seemed more of an XML-bashing page rather than a comparison page. So I thought I should ask here:
What are the benefits of JSON as compared to XML, and why (if at all) should we choose one over the other?
-
Felix Kling about 13 yearsThere are a lot of similar questions already: stackoverflow.com/search?q=json+vs+xml
-
-
Lightness Races in Orbit about 13 yearsJSON is "not only JS", too. Anything can support JSON, regardless of its ancestry.
-
Guffa about 13 yearsWell, XML is much more expressive than JSON, actually. It can for example describe it's own data structure, and it can use namespaces to partition data, features that simply isn't built into JSON.
-
bensiu about 13 years@Tomalak - very true, and soon would be everywhere, but originally designed for JS (JavaScript Object Notation)
-
Lightness Races in Orbit about 13 yearsJSON was not "designed for" JS. It's a markup language that derives from existing Javascript syntax: specifically, Javascript's notation for object declaration.
-
João dos Reis almost 8 yearsIs JSON actually more compact once both have been compressed?
-
Raquel almost 8 yearsThere are even cases when xml is shorter than json as you don't need to wrap element sets with an array. And the ability to have comments in XML is really handy, especially for config files.
-
Raquel almost 8 yearsIn xml you don't need to write the end tag if you have no child elements. Using attributes and element sets can even allow xml to be shorter than json. Xml also supports comments.
-
Lightness Races in Orbit almost 8 years@MatthewWhited: Ah but comments have no place in a wire format!
-
Raquel almost 8 yearsThey may... If you are making a self describing API, remote configuration or just want human readable metadata comments can be very useful.
-
Lightness Races in Orbit almost 8 years@MatthewWhited: A self-describing API shouldn't need comments ;)
-
Raquel almost 8 yearsGood luck with enumerations and other validations.
-
Lightness Races in Orbit almost 8 years@MatthewWhited: Thank you!
-
Addison Klinke over 3 yearsFrom my experience, JSON also has schema support, albeit it would usually be provided in an accompanying file. Is there some feature of XML schema that distinguishes it from JSON schema?