JSON or XML, which format to choose?

With this comparison of JSON and XML, we will attempt, by highlighting the strengh and the weakness of each format, to help you to choose the right one for a given application, and especially for an Ajax/HTML 5 application.

JSON

JSON is a recursive format compatible with JavaScript and that actually is similar in structure to a JavaScript object. It is an object stored into a file. It dates back to 2002 and suddently gained popularity when Ajax become widely used. JSON files can be stored in a PostgreSQL database, and there is a W3C standard to represent form data in JSON, among many other applications.

JSON presumes you know the structure of the document. When used in any programming language, data are used through the structure of an object

1) JSON and JavaScript

JSON is very easy to use in JavaScript, it is a part of the language.
To use a JSON file, you have just to load the file as a text, this is the content of the responseText attribute in Ajax.
Then you use the eval() JavaScript function to transform it into JavaScript object:

var doc = xhr.responseText;
var jdoc = eval('(' + doc + ')');

or with a recent browser:

var jdoc = JSON.parse(doc);

Once the file is parsed, it is used as any JavaScript object:

var value = jdoc.commands[0].value;
var action = jdoc.commands[0].action;
Several scripts are available on the Web to serialize a JavaScript object into a JSON file. With a recent browser, the object is converted to a string:
var str = JSON.stringify(jdoc);

2) JSON and PHP

You can use JSON in PHP once parsed with a PHP parser. A PHP library exists for JSON, you have just to configure php.ini to use it. There is also a json.php library to include directly with the "require" function.

3) Web service

The JSON Web service library is very popular. It is even supported by the Silverlight virtual machine.

4) Transformations

Transformation from a format to another one is accomplished by serializing the object in memory into the new format.

5) Database

JSON is a datatype for PostgreSQL.

6) Example of JSON file

[ { 
"menu": "File", 
"commands": [ 
 {
    "value": "New", 
    "action":"CreateDoc"     
 },
 {
    "value": "Open", 
    "action": "OpenDoc" 
}, 
{ 
    "value": "Close", 
    "action":  "CloseDoc"
 } ]
 } ] 

XML

XML is a markup language that is the base format of a lot of standards and interface languages: RSS, SVG, OPML, XHTML, Open XML, XAML, etc. It allows to describe and analyze any kind of documents, but binary ones, and store them into a file.
It is more verbose than JSON, but a lot of tools are available to process it and it is also the format of documents for word processors and other desktop software.
The structure of XML is free. Any textual content can be expressed in XML and accessed by ID or name. But the format consumes a lot of space.

Using DOM's method may be tedious with XML and slow with XHTML.

1) XML and JavaScript

Unlike JSON, the file is loaded in Ajax directly as an XML document, this is the responseXML attribute of Ajax.
The content is then accessed by DOM's methods.

var xdoc = xhr.responseXML;
var x = xdoc.getElementById("sometag");

2) XML and PHP

XML is part of the core PHP 5 language, it is used directly in PHP through the DOMDocument class and the SimpleXML class. You can load an XML file, process it with DOM's methods and save it directly into a file.

3) Web service

This is an XML format for Web service: SOAP. It is a W3C standard but rather intricate and not very popular.

4) Transformations

Transformation from an XML document to another format may be accomplished via XSLT, not a very easy to use tool. Of course you can also in PHP or JavaScript, load the XML file, get the data and build a file in another format, in some cases this is more easy.

5) Database

The combination of XML and XPath allow to use XML as database, it is thus convenient for large resources.

6) Example of XML file

<?xml version="1.0" ?>
<menubar>
   <menu name="File">
      <command value="New" action="CreateDoc" />
      <command value="Open" action="OpenDoc" />    
      <command value="Close" action="CloseDoc" />
   </menu>
</menubar>

We may even use a more compact form which reflects exactly the object:

<command>
    <value>New</value>
    <action>CreateDoc</action>
</command> 

Conclusion

JSON is simpler to get data from the server and use it as a persistent memory for a program. You have to know the structure of the data to use it, you must be the owner of the data file preferably.
It is lighter than XML and save resources.

XML is better for presentation purpose. It is the language of several graphical user interfaces for now: XAML, XUL, MXML, etc., while QML is similar to JSON. In the first case, the data is stored in a form and used in another form.
You can use XML from external sources and even make XML databases.