[OT] is JSON all that great? - was Re: API Help

Chris Angelico rosuav at gmail.com
Thu Jun 15 00:21:49 EDT 2017


On Thu, Jun 15, 2017 at 12:33 PM, Michael Torrie <torriem at gmail.com> wrote:
> To me JSON seems to hold no real benefits over other serialization
> techniques, including the XML beast. XML may be verbose, but at least
> XML data can be formally validated and formally transformed and queried,
> which is certainly not the case with JSON as we can see from this long
> thread.  JSON's mantra seems to be try parsing it and see what happens.
> Whether it works depends on the parser and any quirks it has (quotes vs
> single quotes, etc).  I don't find JSON any more human readable than XML
> to be honest, perhaps less so because there's no way to define a formal
> schema to follow, at least that I'm aware of.

There are JSON schema validators. And I'd trust them every bit as much
as I'd trust an XML schema validator - probably more, because I can
actually understand them (due largely to the simplicity of JSON).

XML is a document format, like HTML and friends. It has certain
features that JSON doesn't have, and JSON has features that XML
doesn't. Thing is, most people's data is either strictly tabular, or
is structured in something like a Python object hierarchy - not a
document. XML is thus poorly suited to *most* forms of data, and
you'll end up shoehorning data into format and vice versa, which leads
to stupidities like being unable to adequately represent a one-item
list/array, or having to try to maintain element order. Consider:

<?xml version="1.0" encoding="UTF-8" ?>
<spam>
  <asdf>aa</asdf>
  <qwer>qq</qwer>
  <qwer>qw</qwer>
  <qwer>qe</qwer>
  <asdf>as</asdf>
</spam>

What does this represent? A generic XML parser has to cope with it. I
gave this to a few XML-to-JSON converters, and they all interpreted it
as some variant of {"asdf":["aa","as"],"qwer":["qq","qw","qe"]} - but
if you remove the last <asdf> element, they all saw it as
{"asdf":"aa","qwer":["qq","qw","qe"]}. So we lose element order (which
is a feature of XML but not JSON), and we lose single-element arrays
(which are a feature of JSON but not XML), and we make a total hash of
the notion of a tree (since both formats are trees but in slightly
different ways).

Pick a transport format that matches your data structure, or at very
least, a strict superset thereof (eg tabular data is probably best
expressed as CSV, but JSON is perfectly capable of representing it).

XML just needs to die.

ChrisA



More information about the Python-list mailing list