.env to JSON Converter

Convert between .env files and JSON, from text or a file.

About .env to JSON Converter

Convert a .env file to JSON or JSON to .env in the browser, with quotes, comments, multi-line values and duplicate keys handled as you type.

How to use

  1. Paste the contents of a .env file or a JSON object, or load a file (up to 5 MB).
  2. Input that starts with { is read as JSON and converted to .env; anything else is read as .env. Use the Auto / .env → JSON / JSON → .env switch to force a direction.
  3. The output updates as you type. Copy it, or Download it as a .json or .env file.

What it does

From .env to JSON:

  • Each NAME=value line becomes a key with a string value. Values are never turned into numbers or booleans, so PORT=8080 gives "8080".
  • Blank lines and lines that start with # are skipped, a leading export is ignored, and spaces around = are allowed. Names may contain letters, digits, _, . and -.
  • An unquoted value is trimmed, and a # that follows a space starts a comment, so KEY=a#b keeps a#b. NAME= gives an empty string.
  • A single-quoted value is taken literally. A double-quoted value has \n, \r, \t, \" and \\ expanded. Both may run over several lines.
  • When a name appears more than once the last value is used, and a note under the output names it.
  • A line without =, an unclosed quote and text after a closing quote are reported with the line number.

From JSON to .env:

  • The root must be an object. Nested keys are joined with _ and array items take their index: {"db": {"host": "x"}} becomes db_host=x and {"tags": ["a"]} becomes tags_0=a. Names are not changed to upper case.
  • A value is written bare when it holds only letters, digits and _ @ % + = : , . / -. Otherwise it goes in single quotes, or in double quotes with escapes when it contains a single quote, a line break or a tab.
  • null becomes an empty value, and an empty object or array is written as {} or [].
  • Two values that flatten to the same name, and names with other characters such as spaces, are reported as errors.

It does not expand variables: ${VAR} and $VAR are left as written. Backticks are not treated as quotes, and comments are not carried into JSON.

Example

# database
DB_HOST=localhost
DB_PASS="s3cret#1"
export DEBUG=true
→
{
  "DB_HOST": "localhost",
  "DB_PASS": "s3cret#1",
  "DEBUG": "true"
}

Privacy

A .env file usually holds secrets, so it matters that conversion runs in the page: a dropped file is read locally, no request is made, and nothing is stored.

Frequently asked questions

Do values in a .env file need quotes?
A value without spaces or special characters can be written bare. Quote it when it has spaces at either end, a space followed by #, or a line break. Single quotes keep every character as written, which suits passwords that contain symbols.
How do I put a multi-line value in a .env file?
Wrap it in double quotes and either write each line break as \n or break the lines inside the quotes. This converter reads both forms and writes \n.
Are .env values strings, or can they be numbers and booleans?
They are always strings. Environment variables carry text only, so PORT=8080 and DEBUG=true reach the program as "8080" and "true" and the program has to convert them. The JSON produced here keeps them as strings for that reason.
What happens when a key appears twice in a .env file?
Within one file, common loaders such as dotenv for Node.js and python-dotenv use the last value, and so does this converter. The earlier line has no effect, so the note that names the repeated key is a prompt to delete it.