↑ ↓ select, Enter open, Esc close

Convert a JSON array to CSV with a header row

Caution: This command changes the system. Understand what it does before you run it.

Adjust the values, the command updates live
user@server
jq -r '(.[0] | keys_unsorted) as $k | $k, (.[] | [.[$k[]]]) | @csv' plugins.json > plugins.csv

Reads the keys of the first object as column names, prints them as the first line and then, for each object, the values in the same order. @csv quotes strings and correctly escapes embedded quotes. This is handy for preparing wp plugin list --format=json for a spreadsheet, for example.

Note: If fields contain nested objects or arrays, @csv aborts with an error. An existing target file is overwritten.

Also searched as

  • convert json to csv command line
  • jq json to csv with header
  • save api output as spreadsheet

Related one-liners

All in Text processing

Read first, then run.

The commands on myline.de act directly on servers, files and databases. A wrong path or placeholder can delete data irreversibly or make a server unreachable.

  • All commands are provided without warranty and are not tested on every system.
  • Understand what a command does before running it, and check every placeholder.
  • Make a backup first and, if possible, try it on a test system.
  • You run commands at your own risk. Liability for damages is excluded to the extent permitted by law.