Skip to content

Instantly share code, notes, and snippets.

@guillermodlpa
Created March 7, 2023 12:53
Show Gist options
  • Select an option

  • Save guillermodlpa/436e49ee254139c8b6a2a2cbcb384a75 to your computer and use it in GitHub Desktop.

Select an option

Save guillermodlpa/436e49ee254139c8b6a2a2cbcb384a75 to your computer and use it in GitHub Desktop.
Snippet of code to format a plain JSON encoded object into env var file format (KEY=value) using jq
#!/bin/sh
# Formats something like
#
# '{"MODE":"dev","HOST":"db"}'
#
# to
#
# MODE=dev
# HOST=db
#
# Using printf and the quotes around the variable are important to preserve spaces and \n chars in the values
printf '%s\n' "$JSON" | jq -j "to_entries|map(\"\(.key)=\(.value|tostring)\n\")|.[]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment