Convert .env to JSON or YAML: Quoting, Comments and Duplicate Keys
Last updated: · 8 min read
A .env file is a plain text file that keeps application settings as KEY=VALUE lines. The format looks trivial, but it has no formal standard: how quotes, the # character and backslashes are read differs from one library to the next. When you move the same settings into a Kubernetes ConfigMap, a CI variable list or a JSON configuration file, those details can change a value without any error: a trailing comment leaks into the value, or \n does or does not turn into a real line break. This guide goes through the rules the .env to JSON / YAML Converter applies, shows all four conversion directions with examples and states what the tool deliberately does not do (nested objects, ${VAR} expansion). The outputs in the examples are the tool's real results.
Formula
Reading rules (.env → JSON / YAML):
1. Each line has the form KEY=VALUE; a key may contain letters, digits, underscores, dots and hyphens. A leading export and whitespace around = are ignored.
2. Empty lines and lines starting with # are skipped; any other line that is not in KEY=VALUE form is reported with its line number.
3. Unquoted value: leading and trailing whitespace is trimmed; a # at the start of the value or after whitespace begins a comment (the # in abc#def is part of the value).
4. Single quotes and backticks: the content is taken literally. Double quotes: the escapes \n, \r, \t, \" and \\ are resolved. A quoted value may span several lines.
5. If a key is repeated, the last value wins and a warning is added.
6. Every value is text: a double-quoted string in JSON; in YAML, values that look like a number or true/false are quoted so that they stay strings.
Writing rules (JSON / YAML → .env):
7. The root value must be a flat object; values may be strings, numbers, true/false or null (null becomes an empty value).
8. Values containing whitespace, #, quotes, a backslash or a line break are quoted; all others are written bare.
Input 1 (.env):
# Application settings
APP_NAME="My App"
APP_ENV=production
PORT=3000
DEBUG=false
DATABASE_URL=postgres://app:secret@localhost:5432/app
API_KEY='abc123#xyz'
Output 1a (.env → JSON):
{
"APP_NAME": "My App",
"APP_ENV": "production",
"PORT": "3000",
"DEBUG": "false",
"DATABASE_URL": "postgres://app:secret@localhost:5432/app",
"API_KEY": "abc123#xyz"
}
Output 1b (.env → YAML):
APP_NAME: My App
APP_ENV: production
PORT: '3000'
DEBUG: 'false'
DATABASE_URL: postgres://app:secret@localhost:5432/app
API_KEY: abc123#xyz
Input 2 (.env):
export API_URL=https://api.example.com # base URL
GREETING="Hello\nWorld"
RAW='Hello\nWorld'
EMPTY=
PORT=3000
PORT=8080
not a pair
HOME_DIR=${HOME}/app
Output 2 (.env → JSON):
{
"API_URL": "https://api.example.com",
"GREETING": "Hello\nWorld",
"RAW": "Hello\\nWorld",
"EMPTY": "",
"PORT": "8080",
"HOME_DIR": "${HOME}/app"
}
Warning: PORT is defined more than once (lines 5, 6); the last value was used.
Warning: line 7 is not in KEY=VALUE form and was skipped: not a pair
Input 3 (JSON):
{"APP_NAME":"My App","PORT":3000,"DEBUG":false,"API_KEY":"abc123#xyz","EMPTY":null,"MOTD":"line1\nline2"}
Output 3 (JSON → .env):
APP_NAME="My App"
PORT=3000
DEBUG=false
API_KEY="abc123#xyz"
EMPTY=
MOTD="line1\nline2"There is no formula here, only a set of reading and writing rules. They follow common dotenv practice, and Node.js's own documentation describes the same core behaviour: quotes are removed from the value, # starts a comment, an export prefix is ignored, quoted values may span lines and every value is read as text. There are differences too: the Node.js documentation allows only letters, digits and underscores in variable names, whereas this tool also accepts dots and hyphens. The quotes in the YAML output come from the YAML 1.2.2 core schema, under which a bare 3000 would resolve to an integer and false to a boolean.
What the inputs mean
- .env, JSON or YAML
- Depending on the chosen direction: .env text, a flat JSON object or a flat YAML mapping. Up to 1,000,000 characters are processed. If the .env text does not contain a single valid KEY=VALUE line, the tool returns an error.
- Conversion
- One of four directions: .env → JSON (default), .env → YAML, JSON → .env, YAML → .env. The tool does not guess the direction; the input has to match the selection.
Worked example: Converting a .env file to JSON and catching the problem lines
- The first line of Input 1 is a comment and is skipped. In APP_NAME="My App" the double quotes are removed from the value: "APP_NAME": "My App".
- PORT=3000 and DEBUG=false are unquoted, but they do not become a number or a boolean in JSON: "PORT": "3000" and "DEBUG": "false". Environment variables are always text.
- API_KEY='abc123#xyz' is in single quotes, so the # is not a comment and the value is kept as abc123#xyz.
- Converted to YAML, the same input gives PORT: '3000' and DEBUG: 'false' in quotes; left bare, a program reading the YAML would take them as a number and a boolean.
- In Input 2 the export in export API_URL=… is dropped, and the comment # base URL after the whitespace stays out of the value. GREETING is double-quoted, so \n becomes a real line break ("Hello\nWorld" in JSON); RAW is single-quoted, so it stays a backslash followed by the letter n ("Hello\\nWorld" in JSON).
- PORT is defined twice; the last value, 8080, is used and the tool reports: Warning: PORT is defined more than once (lines 5, 6); the last value was used. The seventh line is not in KEY=VALUE form: Warning: line 7 is not in KEY=VALUE form and was skipped: not a pair
- The ${HOME} in HOME_DIR=${HOME}/app is not expanded; the value stays "${HOME}/app".
- Input 3 goes the other way. "My App" is quoted because of the space and "abc123#xyz" because of the #; 3000 and false are written bare; null becomes an empty value (EMPTY=); the value containing a line break is written with an escape as MOTD="line1\nline2".
Result: Input 1 yields a JSON object with six keys, all of whose values are strings. Input 2 yields six keys and two warning lines, with PORT set to "8080". Input 3 becomes six lines of .env text.
🗂️ .env to JSON / YAML Converter
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
Neither direction carries type information. Every value coming from .env is a string: in JavaScript, process.env.DEBUG is the text "false", which counts as true in an if condition, and PORT is the text "3000". Going from JSON to .env, the number 3000 and the string "3000" produce the same line, and both come back as strings. If another program is going to read the .env file, check that program's rules separately: some libraries expand ${VAR} references and some resolve escapes inside double quotes differently; this tool performs no expansion. On security: the conversion runs in your browser and the content is not sent anywhere. Even so, replace real passwords, access keys and database URLs with sample values before pasting them into any website. Never commit .env files to version control: list the file in .gitignore and commit only a .env.example whose values are empty or samples. Deleting a key that was shared by mistake is not enough; rotate it. This is general information.
Common mistakes
- Assuming DEBUG=false is falsy in code. The value is the string "false" and, like every non-empty string, it is truthy; compare explicitly with === "true".
- Trying to convert nested JSON to .env. For the input {"db":{"host":"localhost"}} the tool reports: Nested values cannot be converted to .env: db is an object or array. Only strings, numbers, true/false and null are supported. Flatten the keys first, for example to DB_HOST.
- Mishandling # in unquoted values. In KEY=a #b the value is a, because a # after whitespace starts a comment; if the # belongs to the value, quote it.
- Leaving a quote open. For the input KEY="unclosed the tool stops: Line 1: the quoted .env value is never closed.
- Expecting \n to become a line break inside single quotes. Escapes are resolved only inside double quotes.
- Assuming ${VAR} or $VAR is replaced by another variable's value. The tool performs no expansion; the text stays as written.
- Committing the real .env file to a repository or pasting it into a chat window or an error log. A leaked key has to be rotated.
Frequently asked questions
When do I need quotes around a value in a .env file?
When the value contains whitespace, #, a quote character, a backslash or a line break. Use single quotes to keep the content literal and double quotes when escapes such as \n should be resolved. The tool applies the same test when it writes .env: "My App" is quoted, 3000 is left bare.
Which value wins if a key is defined twice?
In this tool the last definition wins, and the lines on which the key repeats are listed in a warning. The util.parseEnv example in the Node.js documentation returns the last value as well. Other libraries may keep the first one, so removing the duplicate from the file is the robust fix.
Can Node.js read a .env file without an extra package?
Yes. According to the Node.js documentation the --env-file=file option was added in v20.6.0 and the util.parseEnv function in v21.7.0 and v20.12.0. If a variable is defined both in the environment and in the file, the value from the environment takes precedence.
How do I write a multi-line value in a .env file?
Put the value in quotes and write the lines as they are, or use \n inside double quotes. The tool reads both; when it writes .env it puts a value containing a line break on one line, in double quotes with \n.
Why is PORT quoted in the YAML output?
.env values are text. Under the YAML 1.2.2 core schema a bare 3000 resolves to an integer and false to a boolean; the tool writes them in single quotes so the value stays a string. If you want a number, remove the quotes yourself.
Is my .env content sent to a server?
No, the conversion happens in your browser. The safest habit is still never to paste real secrets into an online tool; replace them with sample values first.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.