Differences between XML and JSON for data exchange

jsonxml

I'm wondering about the differences between XML and JSON.

Without any regard to existing processing tools and backend systems and merely as a means of representing data is there anything JSON does better than XML or vice versa? Or can they both be used to the same extent ?

I.e. is there anything that could be represented by JSON that XML could not represent or vice versa?

Best Answer

XML and JSON are both capable of transmitting the same data, but which is better depends mostly on what you want to do with it.

This does touch on existing tooling, but you're not likely to be hand rolling parsers for either, so it is relevant.

XML

  • Has better tooling for verifying schema.
  • Has built in support for namespaces.
  • Can be more easily restructured into HTML.
  • Can be queried with XPath, which is really exceptional query language - think SQL for XML. I haven't really seen an equivalent for JSON - but I'd love to hear about it if I'm wrong on this one.

JSON

  • Has significantly lower ceremony (and, at least in my experience, better pretty printers), so when a human has to look at it, it can be easier to read.
  • Is, unsurprisingly, the best format when one end of the transfer is written in JavaScript.
  • Has less legacy, so the chance of being forced to accept malformed JSON is lower. There is also no cultural expectation that you should have to accept malformed input.

Because one is capable of storing anything that can be stored in the other, usability and tooling is about the only criteria that is useful for comparing what it's like to use them.

If all that were wiped away, I'd probably use neither, and transmit the data as chunks of Lisp.

It's lower ceremony than JSON (barely), much easier to transform than XML, and easier to write a parser for than either (which I'd have to do if all the tooling were gone).

Related Topic