Skip to content

Instantly share code, notes, and snippets.

@dbaynard
Last active July 31, 2022 01:06
Show Gist options
  • Save dbaynard/c9743407ccfc95076d52cb8c3d7413a2 to your computer and use it in GitHub Desktop.
Save dbaynard/c9743407ccfc95076d52cb8c3d7413a2 to your computer and use it in GitHub Desktop.
Convert json to csv with jq
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 );
@dbaynard
Copy link
Author

dbaynard commented Mar 21, 2022

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 (or null) 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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment