XML and JSON -- Advantages and Disadvantages?

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).
  • 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

  • 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.

Tags:

Xml

Json