Search code examples
xmljsoniq

Does JSONiq support processing XML?


I've seen statements that JSONiq query language also supports processing XM, but the only example I found is in the JSONiq Wikipedia article. It looks like XML processing is only possible with the "JSONiq extension to XQuery" but this is not the same as JSONiq, isn't it? The JSONiq specification does not include XML syntax, so it should be forbidden?


Solution

  • The core JSONiq language supports only JSON, in the same the core XQuery language supports only XML. However, there are many people who want to query both XML and JSON in the same program, for example to convert one to the other. This is why there are extensions (both ways). Rather than forbidden, I would say that they are optional (and actually encouraged).

    If the data is a mix of JSON and XML, then you can use either the "JSONiq extension to XQuery" or the "XQuery extension to JSONiq." The differences are minor and revolve around convenience. Feature-wise, both are equivalent.

    The exact differences are comprehensively documented here

    A complete grammar with JSONiq, the XQuery extension and even updates and scripting is available here.

    If there is a lot of JSON and a bit of XML, then "the "XQuery extension to JSONiq" is more adequate: you can query JSON with dots and escaping in strings is done with backslashes, and literals like null, true and false are recognized, but any XPath name queries on XML Names must be prefixed by ./ (./foo instead of just foo), etc.

    If there is a lot of XML and a bit of JSON, the "JSONiq extension to XQuery" is more adequate. It is the opposite: the exact, W3C-compliant XPath syntax is supported and string escaping with ampersands, but for example booleans and nulls must be written as true(), false(), null() to not interfere with XPath.

    The Zorba engine supports both of the variants, and you can switch using xquery version "3.0" or jsoniq version "1.0" in the header of the query, with no further configuration: if you use JSONiq, the XQuery extension is always available as well, and if you use XQuery, the JSONiq extension is always available.

    If no header is present, then the extension .xq or .jq is used to infer the language used.

    Whether or not a JSONiq processor supports the XQuery extension is a decision of the software provider. In general, we encourage implementors of a JSONiq engine to support the XQuery extension if they have the resources. Providers who want to keep a very lightweight engine will stick to core JSONiq.

    The other way round, extending an existing XQuery engine with the JSONiq extension is very easy: when we did so in Zorba when we started supporting JSON, it only took us a few days because the JSON data model is so simple. There is a talk by Jonathan Robie sharing experience on this topic on Youtube.