cozodb/cozo

JSON object key order is lost

Opened this issue · 0 comments

In Cozo-Node 0.7.6

    db.run(`
?[input1, output1, input2, output2] := 
input1=$input1,
output1=json($input1),
input2=$input2,
output2=parse_json($input2)
`, {
      input1: { b: 1, a: 2 },
      input2: `{"b":1,"a":2}`,
    }).then((result) => console.log(JSON.stringify(result)));

//   actual: {"headers":["input1","output1","input2","output2"],"rows":[[{"a":2,"b":1},{"a":2,"b":1},"{\"b\":1,\"a\":2}",{"a":2,"b":1}]]}
// expected: {"headers":["input1","output1","input2","output2"],"rows":[[{"b":1,"a":2},{"b":1,"a":2},"{\"b\":1,\"a\":2}",{"b":1,"a":2}]]}

The same issue can be reproduced in Cozo-Web 0.7.6

I understand that JSON spec does not require the key order to be preserved. I just think that it's better to leave the order unchanged, unless there is a strong benefit in reordering them to alphabetical. I'd support your design if you have already evaluated the performance implication and deliberately discarded the key order.

From a user perspective, it would be a nice property of the DB if the value is stable when I write some object and read it back.

For reference:

  • postgres json preserves object key order, though jsonb will discard the order.
  • serde_json has a preserve_order feature to toggle the behavior.