Last active
May 4, 2020 18:27
-
-
Save rpetrenko/9b7ca925b9ca60620055110084b9a6dd to your computer and use it in GitHub Desktop.
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
#!/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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credit for jq crazy query goes to: https://www.freecodecamp.org/news/how-to-transform-json-to-csv-using-jq-in-the-command-line-4fa7939558bf/