Last active
July 31, 2022 01:06
-
-
Save dbaynard/c9743407ccfc95076d52cb8c3d7413a2 to your computer and use it in GitHub Desktop.
Convert json to csv with jq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_csv: | |
( [.[] | keys | .[]] | unique ) as $keys | |
| ( $keys | @csv ) | |
, ( .[] | |
| . as $row | |
| reduce ($keys | .[]) as $key ([]; [.[], ($row | .["\($key)"])]) | |
| @csv | |
); | |
def uniform_array_to_csv: | |
( .[0] | keys_unsorted | @csv) | |
, (.[] | [.[]] | @csv ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converts an array of objects (or an object of objects) to an array of csv rows.
Objects do not have to be uniform, as missing keys result in
null
values in the output — i.e. skipped fields.First, all the keys are extracted from the whole input, and used for a header row.
Then each object in the input array (or input top-level object) gets converted into a data row.
The
reduce
expression runs through each key, adding the corresponding value (ornull
) to the output.If you have a large array of uniform objects, then do not use
to_csv
!Just do the simpler thing —
uniform_array_to_csv
.(This would benefit from some fuzz testing!)