Monday, 13 June 2011

Comparing XML and JSON

There seem to have been, at various times, some argument about which open standard for human-readable data interchange is best: JSON or XML. This blog takes a look at these two standards and compares the two, hopefully without prejudice.

One is Easier to Read than the Other

Whether JSON is easier to read than XML is really a matter of opinion and what you’re more familiar with. Both are easy to read when pretty printing is used. Consider the following equivalent examples:

<?xml version="1.0" encoding="UTF-8"?>
<WineSearch xmlns="http://marin.tips.spring.webservice/schemas">
    <Request>
     <Country>France</Country>
    </Request>
    <Response country="France">
     <Wine>
      <Name>Chateau Roger</Name>
      <Origin>Bordeaux</Origin>
      <Strength>11.3</Strength>
      <Colour>Red</Colour>
     </Wine>
     <Wine>
      <Name>Gewurztraminer</Name>
      <Origin>Alsace</Origin>
      <Strength>12.4</Strength>
      <Colour>White</Colour>
     </Wine>
    </Response>
</WineSearch>

{
    "WineSearch": [{
        "Request": {
            "Country":"France"
        }
    },
    {
        "Response": [
            {
                "Wine": {
                    "Name":"Chateau Roger",
                    "Colour":"Red",
                    "Strength":"11.3",
                    "Origin":"Bordeaux"
                }
            },
            {
                "Wine": {
                    "Name":"Chateau Roger",
                    "Colour":"Red",
                    "Strength":"11.3",
                    "Origin":"Bordeaux"
                }
            }
        ],
        "Country":"France"
    }]
}

For more examples see the JSON website

Language Independence

Both JSON and XML are language independent, which means that tools to manipulate their structure can be written in virtually any language. JSON, you should also remember, was designed to be used by JavaScript for evaluation purposes; indeed, the json.org website has a complete page on this subject

Limited Typing in JSON

JSON has the following types: string, integer, double, boolean, null and object, whereas XML has a large number of predefined types, as defined in the XML schema definition that can be modified and expanded upon to create ever more complex or restrictive types.

You can argue that JSON has an object type, which, by definition can be absolutely anything.

API Support

Both XML and JSON are mature technologies and both have good API support in many different languages. In terms of Java there are the following:

XML
  • xmlbeans
  • jdom
  • xerces
…and many more.

JSON
Take a look at http://www.json.org for a complete list, though Google’s JSON-Simple has an impressive project usage list.

Root Nodes

One overriding facet of XML is that you always need a root node; something that's not true about JSON. For example, the following JSON string, generated by the json-simple API is legal:

{"balance":1000.0,"num":100,"nickname":null,"is_vip":true,"name":"Mr Wiggins"}

...whereas the following XML is not:

<balance>1000.0</balance>
<num>100</num>
<is_vip>true</is_vip>
<name>Mr Wiggins</name>

Ordering Matters?

When using an XML document with a DTD or schema, the order of the elements in that document is important and is determined by the DTD or schema.

With JSON, the order of the of the name, value pairs in a document is determined by the JSON implementation that you’re using to read and write your document. For example, given the following input string:

{"firstName":"John","lastName":"Smith","address":{"streetAddress":"21 2nd Street","postalCode":"10021","state":"NY","city":"New York"}}

then converting it into a JSONObject and back into a string will change the document to:

{"lastName":"Smith","address":{"streetAddress":"21 2nd Street","postalCode":"10021","state":"NY","city":"New York"},"firstName":"John"}

...as Eric Morecambe once said: it's "all the right notes, but not necessarily on the right order".

These are just a few of the points that currently come to mind, but I think that I could be adding more to this in the future.

No comments: