Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aeldar/df4ec4535b549a3d09d81cbbd57c6b81 to your computer and use it in GitHub Desktop.
Save aeldar/df4ec4535b549a3d09d81cbbd57c6b81 to your computer and use it in GitHub Desktop.
Convert ENV to JSON, filtered by prefix "WEB_APP_", with prefix removed, and with Boolean and Number values recognized. `jq` was used.
jq -n \
'env
| to_entries
| map(select(.key | startswith("WEB_APP_")))
| map({
"key": (.key | sub("WEB_APP_"; "")),
"value": (
if (.value | test("^true$"; "i")) then true
elif (.value | test("^false$"; "i")) then false
elif (.value | test("[[:digit:]]+")) then (.value | tonumber)
elif (.value | test("^null$"; "i")) then null
else .value end)})
| from_entries'
@aeldar
Copy link
Author

aeldar commented Aug 15, 2019

E.g., the following ENV:

WEB_APP_STR="hello world"
WEB_APP_BOOL1="true"
WEB_APP_BOOL2="False"
WEB_APP_NUM="12345"
WEB_APP_NULL="null"

should return JSON:

{
  "STR": "hello world",
  "BOOL1": true,
  "BOOL2": false,
  "NUM": 12345,
  "NULL": null
}

@aeldar
Copy link
Author

aeldar commented Aug 15, 2019

Objects are not parsed, because DON'T PLACE OBJECTS INTO ENV!!!

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