.env ↔ JSON
Convert a .env file to a flat JSON object and back. Values are treated as strings both ways. No nesting, no type coercion of numbers/booleans on the JSON->env direction beyond JSON.stringify. Runs on your device.
Runs on your device. The file is never uploaded.
.env to JSON reads every KEY=VALUE line, allowing an optional export prefix, and strips one matching pair of quotes from the value. Blank lines and comment lines are skipped, and so is any line that does not match. Set mode to json-to-env for the return trip. A ${OTHER_VAR} reference is copied as literal text, never expanded.
Questions
Which way does it convert?
Both, chosen by the mode option, which defaults to env-to-json. That direction reads a .env file and gives a flat JSON object printed with 2-space indentation. The json-to-env direction takes a flat JSON object and writes KEY=value lines, one per key, in the order the object lists them.
Which lines of my .env are read?
Lines of the form KEY=VALUE, where KEY starts with a letter or underscore and continues with letters, digits or underscores. An optional export prefix is accepted. Blank lines and lines starting with # are skipped, and any other line that does not match is skipped silently rather than causing an error.
What happens to quotes around a value?
A matching pair of single or double quotes wrapping the whole value is removed, and everything inside is taken as written. There is no escape processing, so a backslash-n inside the quotes stays two characters, and a multi-line value written across several lines will not be read as one value.
Does it expand ${OTHER_VAR} references?
No. Values are copied as written, so a reference to another variable stays as literal text in the JSON. Nothing is resolved, substituted or read from your actual environment, and no shell is involved. That also means a value which depends on another one will not be expanded on the way back either.
What does the JSON to .env direction do with quoting?
It quotes a value only when it contains whitespace, a double quote or a hash, and escapes any inner double quotes. Values that are not strings are written through JSON stringify, so numbers and booleans become their plain text and an object becomes JSON on one line. A top-level array or non-object is refused with "expected a flat JSON object of key/value pairs".
Is a round trip lossless?
The keys and values survive; the presentation does not. Comments, blank lines, the export prefix and the original ordering of quoting are not preserved, and if a key appears twice the last one wins. Also, your .env likely holds secrets, which is a good reason to note that this runs entirely in a Web Worker on your device.