Data & Formats

.env ⇄ JSON Converter

Turn a .env file into JSON, or write a JSON object as .env lines with correct quoting. Comments, export prefixes, quoted values and multi-line values are understood.

Guide: .env File to JSON: Quoting, Comments and Multi-line Values

Ready.

What it does

A .env file holds environment variables as NAME=value lines. This tool reads them into a JSON object, or writes a JSON object back as a .env file, quoting values that contain spaces, quotes, # or line breaks.

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 \n and \" and may span several lines.
  • An optional export prefix is ignored, and a name with no = becomes null.
  • Variable references such as ${OTHER} are not expanded: values are kept exactly as written.

Good to know

Every value in a .env file is text, so numbers and booleans become strings in JSON. Never commit real secrets to source control, and avoid pasting production secrets into any tool you do not control; this one runs locally in your browser.

Example

PORT=8080, NAME="My App" and D=x # note become {"PORT": "8080", "NAME": "My App", "D": "x"}. A line that is only a name, such as C, becomes null, and E=#x keeps its # because no space precedes it. If a name appears twice, the last value wins and the key keeps its first position.

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 D="[1,2]", because a .env file has no list type.
  • 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, the JSON Formatter points to the exact position of the error.

A .env file often holds secrets, so keep it out of version control by listing it in the Gitignore Generator. For the same data in other configuration formats, see the YAML ⇄ JSON Converter and the TOML ⇄ JSON Converter.

Example: .env to JSON

This file has comments, an export prefix, quotes, an empty value, a multi-line value and a repeated name:

# 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

It converts 7 variables:

{
  "DB_HOST": "override",
  "DB_PORT": "5432",
  "NAME": "My App",
  "GREETING": "it's",
  "EMPTY": "",
  "URL": "https://example.com/?a=1#frag",
  "MULTI": "line1\nline2"
}
  • The word export is accepted, quotes are removed and the multi-line value keeps its line break as \n.
  • A comment after a space is removed, but the #frag inside the URL is kept, because it is not preceded by a space.
  • When a name appears twice, the last value wins, as in most dotenv loaders.
  • Every value is a string. A .env file has no types, so 5432 becomes "5432".

JSON to .env

The other direction flattens nested objects with underscores and uppercases the names. This JSON: {"db":{"host":"localhost","port":5432},"debug":true,"name":"My App","empty":null,"list":[1,2]} becomes:

DB_HOST=localhost
DB_PORT=5432
DEBUG=true
NAME="My App"
EMPTY=
LIST="[1,2]"

Values with spaces are quoted, null becomes an empty value and a list is written as a quoted JSON string, because a .env file cannot hold structures.

Frequently asked questions

Is it safe to paste real secrets here?

The conversion runs in your browser and nothing is uploaded. Still, avoid pasting production secrets anywhere you do not have to, and check files with the Secret Scanner before you share them.

Why are all values strings?

Because that is all a .env file contains. Convert numbers and booleans in your own code after loading them.

Does it support variable expansion such as $HOME?

No. Values are converted as written, without expanding variables.

Privacy

This tool runs entirely in your browser. What you enter is processed on your device and is not sent to a server.

Share this tool

Your data is not included in this link.