Search code examples
xmljson

XML and JSON -- Advantages and Disadvantages?


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?


Solution

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