XML – JSON – Which is best?

The simple answer to this question is – both, now what’s the real question.

The real question is far more complicated and should be something along the lines of “Is XML or JSON best to do …”. Given the specific context of what and how data requires formatting, it is easier to identify the most appropriate technology because there is no real default. They both have there advantages and disadvantage – some of which are identified later.

As of last year both XML (eXtensible Markup Language) and JSON (JavaScript Object Notation) are standardised (XML through W3C and JSON through ECMA-404). Advocates of one can easily pick holes in the other and there are many other data formatting techniques (see the list below) from which to choose. However, XML and JSON are (arguably) the most prevalent and useful in the web space.

Other formats:

  • Abstract Syntax Notation One (ASN.1) – Variety of encoding rules including XML
  • YAML Ain’t Markup Language (YAML) – Super-set of JSON
  • Simple Declarative Language (SDL)
  • Virtual Token Descriptor (VDT)
  • Simple Outline XML (SOX) – Compressed XML

Some of the pros and cons of XML and JSON include:

  • As of the end of last year JSON had a defined mechanism for defining a JSON formatted object in JSON-Schema but it had not been standardised.
  • XML is a more verbose format but not without purpose. The syntax allows for the recording of relationships in a way that is meaningful to the dataset itself and not just as a property of an object or element in an array.
  • XML is a collection of technologies including XSD (Schema definition), XSL-T & XSL-FO (Transformation techniques), XQuery, XPath, the list goes on.
  • JSON has also spun off a number of associated techniques such as JSON-LD (Linked Data sets), JSON-RPC and, not to forget, JSONP (JSON with Padding). The latter being a device for circumventing a web browser’s same-origin security policy (SOP) when issuing an AJAX call to a foreign domain. This issue has since been eased through the adoption by the leading web browsers of Cross-origin resource sharing (CORS), which became a W3C recommendation early this year.

Back in year 2000/2001, in my first experience of what became AJAX, there was little alternative to using XML as the data formatting medium between web browser and server. Besides, it could be so easily transformed to the target format down-stream using XSL-T. In its early days JSON was not well supported and used vulnerable techniques such parsing the JSON string into a JavaScript object using the JavaScript eval statement.
Nowadays, JSON is a well and widely supported format and is often the default position. However, between distrusted parties, over unreliable communications or between vastly different technologies, XML provides a standards-base, validate-able (XSD) and adaptable (XSL-T/-FO) data format.

So, in brief to answer the original question – it depends!

Return to Articles

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s