Skip to content

Instantly share code, notes, and snippets.

@rpetrenko
Last active May 4, 2020 18:27
Show Gist options
  • Save rpetrenko/9b7ca925b9ca60620055110084b9a6dd to your computer and use it in GitHub Desktop.
Save rpetrenko/9b7ca925b9ca60620055110084b9a6dd to your computer and use it in GitHub Desktop.
#!/bin/bash
# json should be any list of dictionaries with
# each item having the same keys
# example:
#[
# {
# "name": "server1.example.com",
# "online": true,
# },
# {
# "name": "server2.example.com",
# "online": false,
# }
#]
#
# jq installation is required: https://stedolan.github.io/jq/download/
if [ "$#" -ne 1 ]; then
echo "Arguments: <fname.json>"
exit 1
fi
cat $1 |jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
@rpetrenko
Copy link
Author

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