Hi team,
Found one "issue" with key ordering in the resulting map. Need to clarify, is it normal behavior or a bug.
json:parse('{"b":1, "a":3}', { 'format': 'xquery' }) returns
{"a":3.0e0,"b":1.0e0}
but
json:parse('{"b":1, "a":3}')<json type="object"><b type="number">1</b><a type="number">3</a></json>
As you see, the standard function returns keys not in the order as it was set in the original JSON string. I didn't find any requirement in xQuery spec that the resulting map entries should be sorted by the key name.
Is it a bug or a feature?
If a feature I would be really happy to see one additional param to preserve key ordering.
There is a big debate in the working group developing XQuery, XPath and XSLT 4 on whether to introduce a new map type that preserves key insertion/creation order or whether even change the existing map type to do that as an option or by default.
Interestingly enough only recently Christian Grün announced that the BaseX fiddle has experimental support for maps preserving key creation order so there, with standard XPath functions, I currently get the result
{
"b": 1,
"a": 3
}
{
"a": 3,
"b": 1
}
for
let $map1 := parse-json('{"b":1, "a":3}'),
$map2 := parse-json('{"a":3, "b":1}')
return
($map1, $map2) =!> serialize({'method':'json',
'indent':true()})
In terms of the current version 3.1 of XPath and XQuery the XDM
3.1 map type basically doesn't prescribe any order of map/map
entries so for that version it is a feature that you can't control
or preserve the order. But as I said, in the future it might
change and the BaseX fiddle with some BaseX trunk or 12
implementation already has experimental support.
I don't know about the BaseX specific functions you seem to be
using, wait for Christian or someone else of BaseX to chime in.