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

Preserve order of keys in JSON lists #388

Open
rubenvereecken opened this issue Feb 15, 2024 · 6 comments
Open

Preserve order of keys in JSON lists #388

rubenvereecken opened this issue Feb 15, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@rubenvereecken
Copy link

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 rubenvereecken added the enhancement New feature or request label Feb 15, 2024
@TomWright
Copy link
Owner

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.

@datatraveller1
Copy link

datatraveller1 commented Nov 15, 2024

dasel is a great tool, thank you very much!
I have encountered the same issue when using dasel -r json -w csv
The field order in the CSV is by alphabetically sorted json keys instead of keeping the original JSON key order.
It would be great if this could be fixed soon.

@datatraveller1
Copy link

Hi @TomWright Is there a workaround to keep the original JSON key order?
I have experimented with
dasel -f test.json -r json -w csv "all().mapOf(z,z,a,a,c,c).merge()"
but this doesn't keep the field order either.

test.json:

[
  {
    "z": 1,
    "a": 2,
    "c": 3
  },
  {
    "z": 1,
    "a": 2,
    "c": 3
  }
]

Result:

a,c,z
2,3,1
2,3,1

@TomWright
Copy link
Owner

@datatraveller1 sorry but no there isn't. The issue is in the CSV writer itself.

@TomWright
Copy link
Owner

@datatraveller1 just to note I'm not ignoring this issue. It is resolved in V3 which I plan on releasing soon

@datatraveller1
Copy link

Great thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants