cozodb / cozo

A transactional, relational-graph-vector database that uses Datalog for query. The hippocampus for AI!
https://cozodb.org
Mozilla Public License 2.0
3.24k stars 92 forks source link

JSON object key order is lost #253

Open chuanqisun opened 3 months ago

chuanqisun commented 3 months ago

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: