Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON object key order is lost #253

Open
chuanqisun opened this issue Mar 20, 2024 · 0 comments
Open

JSON object key order is lost #253

chuanqisun opened this issue Mar 20, 2024 · 0 comments

Comments

@chuanqisun
Copy link
Contributor

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant