Skip to content

Instantly share code, notes, and snippets.

@hassaku63
Created September 25, 2024 10:37
Show Gist options
  • Save hassaku63/a9b40cfbaf641ede229ef0a8edca3699 to your computer and use it in GitHub Desktop.
Save hassaku63/a9b40cfbaf641ede229ef0a8edca3699 to your computer and use it in GitHub Desktop.
Expand dotenv to JSON array of name/value object (like ECS task-def)
# blog(jp): https://zenn.dev/hassaku63/articles/b026266e5a82b4
alias jq-dotenv="jq -R 'split(\"\n\") | map(split(\"=\")) | flatten | {\"name\": .[0], \"value\": .[1]}'"
function dotenv-to-json () {
filename=$1
grep -v '^\s*#' ${filename} |grep -v '^\s*$' | \
jq -R 'split("\n") | map(split("=")) | flatten | {"name": .[0], "value": .[1]}'
}
@hassaku63
Copy link
Author

example usage

$ cat <<EOF> text 
FOO=xxx

# ssss
BAR=xxx
EOF

$ dotenv-to-json text
{
  "name": "FOO",
  "value": "xxx"
}
{
  "name": "BAR",
  "value": "xxx"
}

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