JSON to .env: Convert JSON to Environment Variables
To turn a JSON object into environment variables, write one KEY=value line per field. A flat object maps directly. A nested object has to be flattened first, usually by joining the path with an underscore (DATABASE_HOST) or, for .NET, a double underscore (Database__Host). The other option is to keep the whole document as one minified JSON string in a single variable and parse it at startup. Both are shown below with commands you can paste.
Flat JSON to .env with jq
Given a flat object:
This prints one line per key:
That works for dotenv-style loaders, but GREETING=hello world is not safe to source in a shell, because the space splits the value. Add jq's @sh filter to quote every value:
Nested JSON to environment variables
Environment variables are flat strings, so nested objects and arrays need a naming rule. Take this config:
This jq command walks every leaf value, joins its path with _, and uppercases it:
Array items get their index as part of the name. Whatever reads these variables has to know that convention, so pick one and document it. The flatten nested JSON guide covers the other common separators.
One caveat: paths(scalars) skips null values, so a key set to null produces no line at all. If a missing variable and an empty one mean different things in your app, handle nulls explicitly.
appsettings.json to environment variables (.NET)
.NET configuration reads nested keys from environment variables using a double underscore. Microsoft's docs explain that : does not work in variable names on every platform, so __ stands in for it. Database:Host in appsettings.json becomes Database__Host. Same jq approach, different separator, and no uppercasing:
The same thing in Python
No third-party packages needed:
json.dumps(value) wraps each value in double quotes so spaces survive. Be careful with values that contain double quotes: it escapes them as \", and .env loaders do not agree on unescaping that. In a quick test, Node's dotenv package kept the backslashes and Node's built-in util.parseEnv cut the value short at the first escaped quote. For values like that, use single quotes or the one-variable JSON form below.
Storing a whole JSON object in one environment variable
Sometimes you do not want 40 variables. You want one variable holding the config and a single parse at startup. Minify the JSON first so it fits on one line, then wrap it in single quotes:
The JSON minifier will produce that one-line form. Then parse it where you read it:
The dotenv package for Node documents that inner quotes are kept, so JSON={"foo": "bar"} loads as the string {"foo": "bar"}. Single quotes around the value are the safest choice because the JSON's own double quotes need no escaping inside them. If the value is going somewhere that wants an escaped string instead, such as a JSON field or a YAML scalar, the JSON to String tool escapes it for you.
.env to JSON
Going the other way is shorter, because the parsing is already built in.
In Node.js 20.12 or later, util.parseEnv parses .env text with no dependencies:
Every value comes back as a string. DEBUG=true gives "true", not true, so convert types yourself where it matters.
In Python, python-dotenv has dotenv_values, which returns a dict without touching os.environ:
To check the result, paste it into the JSON formatter.
Things that go wrong
- Secrets in the output. A .env file usually holds credentials. Keep it out of git, and do not paste production secrets into online converters that send data to a server. The tools on this site run in your browser, but the safest file is the one you never paste anywhere.
- Types are lost. Everything in an environment variable is a string.
8080,trueandnullall come back as text and need converting. - Newlines in values. Private keys and certificates span several lines. Either keep them in the single-variable JSON form, where newlines are already escaped as
\n, or check how your loader handles multiline values first. - Name collisions.
{"a_b": 1, "a": {"b": 2}}flattens to twoA_Blines. Rename one key before converting.
FAQ
Can a .env file contain JSON?
Yes. Put the minified JSON on one line and wrap it in single quotes, for example CONFIG='{"debug":true}'. Parse it with JSON.parse or json.loads when you read it.
How do I convert a JSON file to environment variables in bash?
Use jq: jq -r 'to_entries[] | "\(.key)=\(.value | tostring | @sh)"' config.json for a flat object, or the paths(scalars) version above for nested JSON. Redirect the output to a .env file. To load it into the current shell as exported variables, run set -a; . ./.env; set +a.
How do I turn a .env file into JSON?
In Node.js 20.12 or later, use util.parseEnv and JSON.stringify. In Python, use dotenv_values from python-dotenv and json.dumps.