{"id":13552140,"url":"https://github.com/decknroll/json2env","last_synced_at":"2025-04-03T03:30:50.440Z","repository":{"id":43858614,"uuid":"459494643","full_name":"decknroll/json2env","owner":"decknroll","description":"Small, awesome pure shell + jq translator for JSON to environment variables. Even works in BusyBox.","archived":false,"fork":false,"pushed_at":"2023-02-01T00:37:12.000Z","size":47,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-09T00:08:42.943Z","etag":null,"topics":["alpine","alpine-linux","bash","dash","docker","json","shell"],"latest_commit_sha":null,"homepage":"https://hub.docker.com/r/decknroll/json2env/","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/decknroll.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-15T08:38:34.000Z","updated_at":"2024-03-11T19:12:17.000Z","dependencies_parsed_at":"2023-02-17T00:20:22.506Z","dependency_job_id":null,"html_url":"https://github.com/decknroll/json2env","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decknroll%2Fjson2env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decknroll%2Fjson2env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decknroll%2Fjson2env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/decknroll%2Fjson2env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/decknroll","download_url":"https://codeload.github.com/decknroll/json2env/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":213520310,"owners_count":15599595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alpine","alpine-linux","bash","dash","docker","json","shell"],"created_at":"2024-08-01T12:01:59.520Z","updated_at":"2024-08-01T12:12:21.662Z","avatar_url":"https://github.com/decknroll.png","language":"Shell","funding_links":[],"categories":["Shell"],"sub_categories":[],"readme":"# json2env\n\n[![](https://images.microbadger.com/badges/image/decknroll/json2env.svg)](https://microbadger.com/images/decknroll.com/json2env)\n[![](https://img.shields.io/docker/pulls/decknroll/json2env.svg?style=plastic)](https://hub.docker.com/r/decknroll/json2env/)\n[![](https://img.shields.io/docker/stars/decknroll/json2env.svg?style=plastic)](https://hub.docker.com/r/decknroll/json2env/)\n[![](https://img.shields.io/badge/docker_build-automated-blue.svg?style=plastic)](https://hub.docker.com/repository/docker/decknroll/json2env/builds)\n\nDockerHub: [json2env](https://hub.docker.com/r/decknroll/json2env/)\nGitHub: [json2env](https://github.com/decknroll/json2env)\n\nKilna's swiss army knife for turning JSON objects into eval-able environment\nshell code for setting environment variables.\n\nThere are a few projects named `json2env` out there, this one attempts to\nmeet the following goals:\n\n* Runs under Alpine Linux\n* Has only POSIX or busybox tools, plus `jq` as its preprequisites\n* Runs under `dash` (busybox/alpine), `bash`, `ksh` and `zsh` shells\n* Can output JSON arrays as:\n  * Native shell arrays\n  * Delimited strings\n  * JSON string (compact or pretty)\n* Can output JSON objects (dicts/maps) as:\n  * Bash-style associative arrays\n  * Delimited strings\n  * JSON string (compact or pretty)\n* Processes input from files or STDIN\n* Outputs to STDOUT or a file\n* Environment variable name options\n  * Change case of the var name(s)\n  * Prefix the var name(s)\n  * Translate all or only one variable\n    * Optionally use a different name than the JSON key\n* Can select a subpath of the JSON to export\n* Translate with or without export of environment variables\n\nThis covers the majority of the use cases for shell-based build and\nautomation that needs access to JSON data.\n\n## Usage\n\n```\nUSAGE: json2env [ OPTIONS ] [ FILENAMES ]\n\nKilna's swiss army knife for turning key-values in a JSON object into eval-able\nshell code for setting environment variables.\n\nProcesses FILENAMES as JSON documents if provided, otherwise processes standard\ninput as a JSON document.\n\n         --export : Export shell variables\n               -x   (prepend each key=val with shell's export keyword)\n\n      --path PATH : JSON sub-path in jq .path.to.the.object dot-prefix notation\n          -p PATH   (e.g '.env_vars') - defaults to '.' for the root JSON object.\n                    The referenced path must only be a JSON object (dict/map/hash),\n                    not an array, string, etc.\n\n  --prefix PREFIX : Prepend this string the to names of output shell variables\n        -p PREFIX\n\n          --upper : Translate JSON keys into uppercase shell environment vars\n               -u\n\n          --lower : Translate JSON keys into lowercase shell environment vars\n               -l\n\n        --key KEY : Only output this single key in the object\n           -k KEY   (defaults to all keys)\n\n  --env-name NAME : When outputting a single key with --key, force this value\n          -e NAME   as the environment variable name\n\n           --text : JSON objects and arrays become newline delimited text\n               -t   rather than the default behavior of setting the shell\n                    environment variable to a JSON string representation\n\n  --kv-sep STRING : Key-value separator for list-style translation of JSON\n        -K STRING   objects (defaults to ':')\n\n--list-sep STRING : Record separator for list-style translation\n        -L STRING   (defaults to newline)\n\n          --array : JSON arrays are translated into POSIX shell native arrays\n               -a   (overrides --text)\n\n          --assoc : JSON objects are tranlated into bash-style native\n               -a   associative arrays (overrides --text)\n\n          --force : Output shell native for --array or --assoc even if the\n               -f   current shell does not support it\n\n         --strict : Fail on JSON keys which aren't alphanumeric + underscore\n               -s   (defaults to translating keys)\n\n  --out-file FILE : Output to a file instead of STDOUT\n          -o FILE\n\n        --compact : Output JSON strings in compact mode\n               -c\n\n           --help : Show help\n               -h\n```\n## Docker\n\nThis project also builds an associated docker image `decknroll/json2env`, so\nyou can run `json2env` without installing locally:\n\n```\n$ cat env.json | docker run -i decknroll/json2env \u003e env.sh\n```\n\nAll of the command line parameters are supported, pass them as options to\n`docker run` the same as if you were calling the script directly:\n\n```\n$ cat foo.json | docker run -i decknroll/json2env --lower --prefix env_ --text \u003e foo.sh\n```\n\nThe Docker image is only 4mb in size.\n\n## Examples\n\n### Saving to a `.env` file\n\nMany tools use a `.env` file to set properties for a given directory. If you\nhave a bunch of parameters in an `environment.json` file:\n\n```\n{\n  \"profile\": \"main\",\n  \"mode\":    \"init\"\n}\n```\n\n...and you want to create `.env` file from it, simply:\n\n```\n$ json2env --out-file .env environment.json\n$ cat .env\nprofile=main\nmode=init\n```\n\n### Setting environment variables for immediate use\n\nIf you need the environment variables available in the currently-running shell,\nsimply `eval` the output of this script:\n\n```\n$ eval $(json2env --export env-vars.json)\n```\n\nMake sure to use `--export` so that any commands run by your shell will inherit\nthe set values.\n\n### Selecting a JSON sub-path\n\nIf you want to set environment variables only for a certain object in the JSON\ndocument `config.json`:\n\n```\n{\n  \"project\": \"foo\",\n  \"environ\": {\n    \"SERVICE_TOKEN\": \"ynubAEtfyHue2DZfNPRSAfSDN34zuvbh\"\n  }\n}\n```\n\nThen you can export only the `environ` key by:\n\n```\n$ json2env --export --path .environ config.json\nexport SERVICE_TOKEN=ynubAEtfyHue2DZfNPRSAfSDN34zuvbh\n```\n\nThe `--path` is provided in `jq` style .path.to.the.object syntax.\n\n### Changing variable names\n\nIf you want to change the case of the var names you can use `--lower` or\n`--upper`:\n\n```\n$ json2env --export --lower .environ config.json\nexport service_token=ynubAEtfyHue2DZfNPRSAfSDN34zuvbh\n```\n\nIf you only need one key as opposed to all keys in a JSON document path, you\ncan specify it using `--key`, and if you want to set an environment variable\nname other than one based on the key, use the `--env-name` option.\n\nYou can also prefix all keys with `--prefix` to avoid name collisions and/or to\ngroup the environment variables.\n\n### Arrays\n\nGiven a file `letters.json`\n```\n{\n  \"alpha\": [\n    \"a\",\n    \"b\",\n    \"c\"\n  ]\n}\n```\n\nWe can see that JSON arrays are by default translated to JSON strings for the\nenvironment:\n\n```\n$ json2env letters.json\nalpha='[\n  \"a\",\n  \"b\",\n  \"c\"\n]'\n```\n\nYou can also compact the JSON syntax:\n\n```\n$ json2env --compact letters.json\nalpha='[\"a\",\"b\",\"c\"]'\n```\n\nJSON isn't very handy for manipulating in the shell, so you can also\nuse `--array` to output as a shell array:\n\n```\n$ json2env --array letters.json\nalpha=('a' 'b' 'c')\n```\n\nWhich you can then use in a `for` loop.\n\nSome minimalist shells don't support arrays (like `dash` which comes with\nAlpine Linux), or sometimes you just want to flatten to a text-based list,\nand that's what `--text` is for:\n\n```\n$ json2env --text letters.json\nalpha='a\nb\nc'\n```\n\nThe default delimiter is newline, if you want to use for example spaces\nor commas instead, use `--list-sep`:\n\n```\n$ json2env --text --list-sep ' ' letters.json\nalpha='a b c'\n$ json2env --text --list-sep , letters.json\nalpha='a,b,c'\n```\n\n### Objects\n\nLikewise given a file `translate.json`\n```\n{\n  \"words\": {\n    \"uno\": \"one\",\n    \"dos\": \"two\",\n    \"tres\": \"three\"\n  }\n}\n```\n\nMuch the same, JSON objects are by default translated to JSON strings for the\nenvironment:\n\n```\n$ json2env translate.json\nwords='{\n  \"uno\": \"one\",\n  \"dos\": \"two\",\n  \"tres\": \"three\"\n}'\n```\n\nJust like with arrays, you can also compact the JSON syntax:\n\n```\n$ json2env --compact translate.json\nwords='{\"uno\":\"one\",\"dos\":\"two\",\"tres\":\"three\"}'\n```\n\nIf you have a modern bash-like shell, you can also use `--assoc` to output an\nassociative array:\n\n```\n$ json2env --assoc translate.json\ndeclare -A words=(\n  [dos]=two\n  [tres]=three\n  [uno]=one\n)\n```\n\nMany shells don't support bash-style associative arrays. So, as with arrays,\nyou can also use `--text` to output objects as lists. The default is a space\nbetween the key and the value, with a newline between each entry.\n\n```\n$ json2env --text translate.json\nwords='dos two\ntres three\nuno one'\n```\n\nAnd you can change the separators as well with `--list-sep` and `--kv-sep`:\n\n```\n$ json2env --text --list-sep ';' --kv-sep '=' translate.json\nwords='dos=two;tres=three;uno=one'\n```\n\n## Known issues\n\n* Input files must come last on the command line\n* Needs better input validation \u0026 error messaging on bad input\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecknroll%2Fjson2env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdecknroll%2Fjson2env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdecknroll%2Fjson2env/lists"}