How to Convert a .env File to JSON (and Back)

By Ramanathan Aug 25, 2026 1 min read .env to JSON Converter

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.

About the author

Ramanathan · Software Engineer & Solutions Architect

I'm a Software Engineer and Solutions Architect with 20+ years of experience building enterprise applications across BFSI, Healthcare, Retail, Manufacturing, and Industrial Automation. I've spent those two decades living in JSON, tokens, regexes, and config files — so I built the fast, private, no-login developer tools I always wanted to reach for myself.

Last updated: Aug 25, 2026