How to Convert a .env File to JSON (and Back)
A .env file holds environment variables as simple KEY=value lines. Converting it to JSON is useful for seeding config, comparing environments, or feeding a tool that expects JSON — and converting JSON back to .env helps you generate one. Here's how the mapping works.
Lines become a flat object
Each KEY=value line becomes one property. Blank lines and # comments are skipped, and an optional export prefix is stripped:
# App config
export API_KEY=abc123
PORT=3000
{ "API_KEY": "abc123", "PORT": "3000" }
Everything is a string
Environment variables are always strings — PORT=3000 is the text "3000", not the number 3000. The JSON output keeps every value as a string so it round-trips back to an identical .env file.
Quotes and escapes
Values can be quoted. Double quotes honor escape sequences like \n, while single quotes are literal:
GREETING="hello\nworld"
LITERAL='raw$value'
On an unquoted line, a trailing # comment is dropped. When converting back to .env, values with spaces or special characters are automatically quoted.
Nested JSON
A .env file is flat, so when you convert JSON that contains nested objects or arrays, each nested value is serialized as a JSON string for that key — keeping the file valid while preserving the data.
Related
Working with other config formats? See TOML to JSON and YAML to JSON.
Try it
Paste a .env file or JSON and convert instantly — everything runs in your browser, so secrets never leave your machine.