.env File to JSON: Quoting, Comments and Multi-line Values
Short answer: for env to JSON conversion, remember that a .env file holds NAME=value lines. The .env ⇄ JSON Converter reads them into a JSON object, or writes a JSON object back as .env lines with correct quoting. Comments, export prefixes, quoted values and multi-line values are understood. Every value in a .env file is text, so numbers become strings in JSON.
How values are read
- Unquoted values end at the line break. A
#preceded by a space starts a comment. - Single-quoted values are literal.
- Double-quoted values understand escapes such as
\nand\", and may span several lines. - An optional
exportprefix is ignored. - A name with no
=becomesnull. - References such as
${OTHER}are not expanded. Values are kept exactly as written.
A worked example
# database
export DB_HOST=localhost
DB_PORT=5432
NAME="My App"
GREETING='it\'s'
EMPTY=
URL=https://example.com/?a=1#frag # trailing comment
MULTI="line1
line2"
DB_HOST=override
converts 7 variables to:
{
"DB_HOST": "override",
"DB_PORT": "5432",
"NAME": "My App",
"GREETING": "it's",
"EMPTY": "",
"URL": "https://example.com/?a=1#frag",
"MULTI": "line1\nline2"
}
Three details to notice:
- the comment after a space is removed, but
#fraginside the URL is kept, because no space precedes it; - when a name appears twice, the last value wins, as in most dotenv loaders;
5432becomes"5432": a.envfile has no types.
JSON to .env
With "Flatten nested objects" on, {"a":{"b":1}} becomes A_B=1. With it off, the nested object is written as a JSON string on one line. Arrays are written as JSON text, for example LIST="[1,2]", because a .env file has no list type. Values with spaces, quotes, # or line breaks are quoted, and null becomes an empty value.
Names may contain letters, digits, _, . and -. Every other character, including accented letters and spaces, is replaced with _, so two keys can end up with the same name. Check the output for repeats.
The input must be a JSON object. If the JSON itself is malformed, see JSON syntax errors.
Secrets
A .env file often holds secrets. Keep it out of version control by listing it in your .gitignore (see the .gitignore guide), and avoid pasting production secrets into any tool you do not control. This converter runs locally in your browser. If a secret was committed or shared, read Leaked API key: what to do.
How to convert env to JSON with the converter
- Open the .env ⇄ JSON Converter.
- Choose
.envto JSON or JSON to.env. - Set "Uppercase names" and "Flatten nested objects" as needed.
- Copy the output and check for repeated names.
For other configuration formats, use the YAML ⇄ JSON Converter (see YAML to JSON conversion) and the TOML ⇄ JSON Converter (see TOML validation).
Limits to know
- No variable expansion.
- No types: everything is a string.
- Name clean-up can create duplicates.
Frequently asked questions
Why is my port a string in JSON?
.env values are text.
Does a # always start a comment?
Only when preceded by a space in an unquoted value.
What if a name appears twice? The last value wins.
Are ${VAR} references expanded?
No.
How are arrays written? As JSON text in quotes.