The “X” in JSON stands for Extensibility
While I do use JSON, I still think XML is superior for some important use cases (long-lived data that may be exchanged with other systems). So here’s a quick “rant” – prompted by Dan Brickley’s response to Bob DuCharme’s Reification is a red herring – aimed at those (younger?) folks “dissing” XML :)
Over time, requirements and technology inevitably change. Extensibility (the “X” in XML) is the property that helps us adapt and keep pace, letting us evolve technology from simple to advanced without having to throw away and reimplement existing functionality. Here’s two reasons why XML is better at it than JSON:
From one to many
Cardinality (whether data has one or multiple values) is a common problem in data modeling: In a relational database, you need to change the schema (creating an additional table) to turn a single-valued into a multi-valued field. Programming languages distinguish between scalar values and arrays or collections.
Not so in XML; I can move effortlessly (on the producer side, at least) from single…
<book>
<author>Alice</author>
</book>
… to multiple without changing the syntax:
<book>
<author>Alice</author>
<author>Bob</author>
</book>
In JSON, if you do it the normal way (not making everything an array) – a single-valued property…
{author:"Alice"}
… needs to be changed to an array to support multiple values:
{author:["Alice","Bob"]}
You could argue that this is one of the things that make XML hard for consumers. But I appreciate that changing the cardinality after a while doesn’t force you to modify your data format.
Another dimension
Attributes – adding additional information about data – is for free in XML (at least one level deep, and if you’re not in an attribute already) ; you can go from…
<author>Alice</author>
… to additional info without changing the format:
<author url="http://example.com/alice">Alice</author>
JSON doesn’t offer that additional dimension; a scalar value…
{author:"Alice"}
… doesn’t support metadata and has to be changed to an object first to support “attributes”:
{author:{name:"Alice",url:"http://example.com/alice"}}
Extensibility FTW
An important feature XML and JSON share is being able to add new fields at will without affecting existing consumers (unless you shoot yourself in the foot with overly rigid schemas or writing very dumb clients). But there’s more that makes XML extensible (e.g., mixed content or namespaces).
So when you design your next data interchange format, consider going XML to future-proof your data. Let me know if you disagree :)
(P.S.: Topic Maps offer an extremely extensible data model…)