Last active
October 10, 2023 21:04
-
-
Save djeikyb/bf33dd6522472df3a662fd5148a6f516 to your computer and use it in GitHub Desktop.
Turns an appsettings.json into flat json of key-value pairs, or, shell friendly list of plain key-value pairs
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/sh | |
toEnvVars() { | |
toFlatJson "$1" | jq --raw-output '. | to_entries[] | "\(.key)=\(.value)"' | |
} | |
toFlatJson() { | |
# thank you jeff mercado https://stackoverflow.com/a/42303836/659715 | |
jq --arg delim '__' 'reduce (tostream|select(length==2)) as $i ({}; | |
.[[$i[0][]|tostring]|join($delim)] = $i[1] | |
)' "$1" | |
} | |
# One of the args should be a json file. | |
# If there's only one arg, then that's the json file. | |
if [ -z "$2" ]; then | |
toFlatJson "$1" | |
exit 0 | |
fi | |
# So we've been passed a file. | |
# Assume it's json. Sorry. | |
# Translate the json file to a shell-friendly list of k=v lines | |
[ "$1" = "-e" ] && toEnvVars "$2" | |
# Translate the json file to a flat object of key value pairs | |
[ "$1" = "-j" ] && toFlatJson "$2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can use like: