Boneyard Tools

The .env format and how it maps to JSON

How dotenv files store configuration, the syntax rules this converter follows, and why every value ends up as a string in JSON.

What a .env file is

A .env file is a plain text list of KEY=VALUE pairs, one per line, that a dotenv library reads at startup to load configuration into a program's environment variables. The pattern comes from the Twelve-Factor App idea that configuration should live in the environment rather than being hard-coded, which keeps secrets and per-machine settings out of the source tree. Because it is just text, the same file can seed a local shell, a container, or a CI job. The format is deliberately minimal, which is exactly why small differences in how values are quoted or commented tend to trip people up.

The syntax rules this converter follows

Each line is trimmed, and lines that are empty or start with # are treated as comments and skipped. An optional leading 'export ' is removed so files meant to be sourced in a shell still convert. The first equals sign splits the key from the value, so any further equals characters, such as those in a database URL, stay part of the value. One matching pair of surrounding single or double quotes is stripped from the value, while the characters inside those quotes, including spaces, are kept exactly.

Why everything becomes a string

Operating systems store environment variables as strings and nothing else, so a dotenv file has no real concept of a number or a boolean. That is why this converter emits "5" rather than 5 and "true" rather than true: keeping them quoted is the honest representation of what your program will actually receive. When you convert JSON back to .env the same flattening happens, and any richer JSON types are turned into their string form before they are written out. If your app needs a real integer or boolean, it must parse the string itself.

Going from JSON back to .env

The reverse direction expects a flat JSON object whose keys become variable names and whose values become the text after each equals sign. A value is quoted only when leaving it bare would be ambiguous, meaning it is empty or holds a space, a #, or a quote character, and inner double quotes are escaped with a backslash. The converter does not re-add the export prefix or any comments, so the output is a clean minimal .env. A top-level array or a non-object input is rejected because it cannot be expressed as simple KEY=VALUE lines.

Frequently asked questions

Does it interpret escape sequences like backslash n?

No. The parser removes one layer of surrounding quotes but keeps the remaining characters literally, so a backslash followed by n stays as those two characters rather than becoming a newline.

Why did my number lose its type in JSON?

Environment variables are always strings, so the converter keeps 5 as "5" to reflect what your program will read. Cast it to a number in your application code after loading it.

Can I store a value that contains spaces?

Yes. Wrap it in single or double quotes in the .env file and the quotes are stripped on the way to JSON. Converting back, a value with spaces is automatically re-quoted for you.