Generate a .env file with the JSON to env converter
The JSON to env converter flattens a JSON object into dotenv lines. Each leaf value becomes one KEY=value line, nested object keys are joined with a separator, and values that would confuse a dotenv parser are wrapped in double quotes with the necessary escapes. It is useful when a secrets manager, CI system or teammate hands you JSON but your app reads environment variables.
Example
Input:
{
"app": { "name": "Acme Shop", "debug": false, "port": 8080 },
"redis-cache": { "url": "redis://cache:6379" },
"features": ["search", "checkout"],
"secret": "p@ss$word"
}
Output with the defaults:
APP__NAME="Acme Shop"
APP__DEBUG=false
APP__PORT=8080
REDIS_CACHE__URL=redis://cache:6379
FEATURES__0=search
FEATURES__1=checkout
SECRET="p@ss\$word"
Options explained
- Nested key separator:
__matches ASP.NET Core and the nesting option in .env to JSON, so the two tools round-trip. Use_for a flatter look, knowing that it can no longer be split back unambiguously. - UPPERCASE keys: on by default, following the usual convention for environment variables. Turn it off to keep the original casing, for example
db__host. - Prefix with export: writes
export KEY=value, so the file can be loaded withsource .envin a shell and the variables reach child processes.
How values are written
- Plain words, numbers and booleans are written as they are.
- Values containing spaces,
#, quotes,=, backslashes, backticks or$are double-quoted.$is escaped as\$so it is not treated as a variable reference. - Line breaks become
\ninside double quotes, keeping each variable on one line. nullbecomes an empty value (KEY=) and an empty string becomesKEY="".
How different loaders read the file
The .env format has no single standard, so check how the tool that reads your file treats quotes before you rely on them:
| Loader | Surrounding quotes | Notes |
|---|---|---|
source .env in bash (with the export option) | Removed | Normal shell rules, so $ must stay escaped |
Docker Compose env_file | Removed | Supports ${VAR} interpolation |
docker run --env-file | Kept as part of the value | Every line is taken literally, so "Acme Shop" includes the quotes |
Node.js --env-file and the dotenv package | Removed | No variable expansion unless you add dotenv-expand |
If a value comes out with stray quotes or backslashes, the loader is the cause, not the conversion. For docker run, keep such values simple or pass them with -e instead.
Edge cases
- Invalid characters in keys: anything other than letters, digits and underscores is replaced with
_, soredis-cachebecomesREDIS_CACHE. - Keys starting with a digit get a leading underscore, because shells reject variable names such as
2FA_ENABLED. - Arrays are expanded into numbered keys (
FEATURES__0,FEATURES__1). If your app expects a single comma-separated value, join the array in the JSON first. - Empty objects and arrays are written as JSON text, such as
EXTRA={}.
For Kubernetes or Docker Compose files that expect structured config rather than flat variables, convert the JSON with JSON to YAML instead.
Privacy
The conversion happens locally in your browser, so API keys and passwords in your JSON are never uploaded.