How to map an object to arrays so it can be converted to csv?

Try this filter:

to_entries[] | [.key, .value]
  • to_entries converts an object to an array of key/value objects. [] breaks up the array to each of the items in the array
  • then for each of the items, covert to an array containing the key and value.

This produces the following output:

[
  "123",
  "abc"
],
[
  "231",
  "dbh"
],
[
  "452",
  "xyz"
]

Then you can use the @csv filter to convert the rows to CSV rows.

$ echo '{"123":"abc","231":"dbh","452":"xyz"}' | jq -r 'to_entries[] | [.key, .value] | @csv'
"123","abc"
"231","dbh"
"452","xyz"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)