We have a program that accepts as data XML, JSON, SQL, OData, etc. For the XML we use Saxon and its XPath support and that works fantastic.
For JSON we use the jsonPath library which is not as powerful as XPath 3.1. And jsonPath is a little squirrelly in some corner cases.
So... what if we convert the JSON we get to XML and then use Saxon? Are there limitations to that approach? Are there JSON constructs that won't convert to XML, like anonymous arrays?
Lots of different people have come up with lots of different conversions of JSON to XML. As already pointed out, the XPath 3.1 and the XSLT 3.0 spec have a loss-less, round-tripping conversion with
json-to-xmlandxml-to-jsonthat can handle any JSON.There are simpler conversions that handle limited sets of JSON, the main problem is how to represent property names of JSON that don't map to XML names e.g.
{ "prop 1" : "value" }is represented byjson-to-xmlas<string key="prop 1">value</string>while conversions trying to map the property name to an element or attribute name either fail to create well-formed XML (e.g.<prop 1>value</prop 1>) or have to escape the space in the element name (e.g.<prop_1>value</prop_1>or some hex representation of the Unicode of the space inserted).In the end I guess you want to select the property
fooin{ "foo" : "value" }asfoowhich the simple conversion would give you; in XPath 3.1 you would need?foofor the XDM map orfn:string[@key = 'foo']for thejson-to-xmlresult format.With
{ "prop 1" : "value" }the latter kind of remains asfn:string[@key = 'prop 1'], the?approach needs to be changed to?('prop 1')or.('prop 1'). Any conversion that has escaped the space in an element name requires you to change the path to e.g.prop_1.There is no ideal way for all kind of JSON I think, in the end it depends on the JSON formats you expect and the willingness or time of users to learn a new selection/querying approach.
Of course you can use other JSON to XML conversions than the
json-to-xmland then use XPath 3.1 on any XML format; I think that is what the oXygen guys opted for, they had some JSON to XML conversion before XPath 3.1 provided one and are mainly sticking with it, so in oXygen you can write "path" expressions against JSON as under the hood the path is evaluated against an XML conversion of the JSON. I am not sure which effort it takes to indicate which JSON values in the original JSON have been selected by XPath path expressions in the XML format, that is probably not that easy and straightforward.