Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created August 1, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaauie/55328d2edf0c3ff374cd83302e470b03 to your computer and use it in GitHub Desktop.
Save yaauie/55328d2edf0c3ff374cd83302e470b03 to your computer and use it in GitHub Desktop.
Script that takes an JSON-blob on stdin containg an array of objects, and outputs a CSV.
#!/usr/bin/env sh
if which jq ; then
cat | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
else
>&2 echo "\033[31mERROR: $(basename $0) requires jq\033[0m"
exit 127
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment