dasel icon indicating copy to clipboard operation
dasel copied to clipboard

Preserve order of keys in JSON lists

Open rubenvereecken opened this issue 1 year ago • 6 comments

Is your feature request related to a problem? Please describe. I deal with JSON lists of objects, which I convert to CSV. The key order between the objects is consistent, yet in the output is random.

Describe the solution you'd like We already have preserved key order for regular objects:

$ echo '{"z":1,"a":2,"c":3}' | dasel -r json -w csv
z,a,c
1,2,3

I would love to have the same feature for lists of objects – ie:

$ echo '[{"z":1,"a":2,"c":3},{"z":1,"a":2,"c":3}]' | dasel -r json -w csv                                                                                                                                                     
z,a,c
1,2,3
1,2,3

Instead, what currently happens is:

$ echo '[{"z":1,"a":2,"c":3},{"z":1,"a":2,"c":3}]' | dasel -r json -w csv                                                                                                                                                     
a,c,z
2,3,1
2,3,1

rubenvereecken avatar Feb 15 '24 08:02 rubenvereecken

This is only an issue when writing to CSV:

$ echo '[{"z":1,"a":2,"c":3},{"z":1,"a":2,"c":3}]' | dasel -r json
[
  {
    "z": 1,
    "a": 2,
    "c": 3
  },
  {
    "z": 1,
    "a": 2,
    "c": 3
  }
]
$ echo '[{"z":1,"a":2,"c":3},{"z":1,"a":2,"c":3}]' | dasel -r json -w yaml
- z: 1
  a: 2
  c: 3
- z: 1
  a: 2
  c: 3

More of a note to myself at the moment - hopefully I can better integrate the CSV parser in use.

TomWright avatar Feb 16 '24 19:02 TomWright