Am 16.12.2024 um 13:42 schrieb Oleksandr Shpak:
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
<https://fiddle.basex.org/?share=%28%27query%21%27let.1*b6157a638%2C0.2*a6357b618Areturn0+%7B91%2C.2%7D+%3D%27%3A%3E+serialize%7B%28%22method%22%3A%22C%225%22indent%22%3Atrue%7B%7D%294%21%27%27%7Emode%21%27XQuery+%7BBaseX4Type%21%27xml%27%29*+%3A%3D+parse-C%7B%22%287.+90A+++4%7D%27%7Econtext5%2C+6%5C%217%5C%278%29%22%7D9%24mapA%5CnCjson%01CA9876540.*_>,
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.