Home → JSON to env Converter

JSON to env Converter

Convert JSON configuration objects to .env environment variable format.

About This Tool

Convert JSON configuration objects to .env environment variable format. This tool runs entirely in your browser — no data is ever sent to a server. Free to use, no account required.

What .env File Format Is

The .env format is the standard way to store environment-specific configuration variables outside of application code.

.env File Syntax

One variable per line in KEY=value format. No spaces around the equals sign. String values can optionally be quoted. Lines starting with # are comments. Variable names are conventionally uppercase with underscores.

How .env Variables Are Used

Dotenv libraries (dotenv for Node.js, python-dotenv for Python) read .env files on application start and make each KEY available as an environment variable. This lets the same codebase run with different configuration in different environments.

Converting JSON Configuration to .env

JSON configuration objects from APIs or secrets managers can be directly converted to .env format for use in deployment pipelines.

Flat JSON to .env

Top-level string, number, and boolean keys become environment variable names (uppercased) and their values become the variable values. This is the most straightforward case and produces clean .env output.

Nested JSON to .env

Nested keys are flattened using double underscore (__) or single underscore separator. {"db": {"host": "localhost"}} becomes DB__HOST=localhost or DB_HOST=localhost depending on your preference.

Frequently Asked Questions

What is a .env file?+
A .env file stores environment variables for an application in a simple KEY=value text format. It is used to separate configuration from code — API keys, database URLs, feature flags, and other settings go in .env files instead of being hardcoded. The dotenv library reads .env files and makes variables available as environment variables at runtime.
Why convert JSON to .env format?+
Many applications store configuration as JSON objects (from APIs, config files, or secrets managers like AWS Parameter Store) but need those values as environment variables for deployment. Converting JSON to .env format lets you take a JSON config response and immediately use it to populate a .env file.
How are nested JSON objects handled in .env conversion?+
Nested JSON objects are flattened using an underscore separator. For example, {"database": {"host": "localhost", "port": 5432}} becomes DATABASE_HOST=localhost and DATABASE_PORT=5432. All environment variable names are uppercased by convention.
Are there any values that cannot be converted to .env?+
Arrays and complex nested structures do not map cleanly to .env format, which supports only scalar values. Arrays can be represented as comma-separated strings, but this requires custom parsing in your application. For complex configuration, JSON or YAML files are more appropriate than .env.

JSON to .env Conversion Example

Converting a JSON configuration object to .env format makes it easy to set up environment variables for Node.js, Docker, and CI/CD pipelines.

JSON Input → .env Output

// JSON Input
{
  "database_host": "localhost",
  "database_port": 5432,
  "database_name": "myapp",
  "api_key": "sk-abc123",
  "debug_mode": true,
  "max_connections": 100
}

# .env Output
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=myapp
API_KEY=sk-abc123
DEBUG_MODE=true
MAX_CONNECTIONS=100

.env Format Rules

TypeRuleExample
StringsNo quotes needed for simple stringsAPI_KEY=myvalue
Strings with spacesWrap in double quotesAPP_NAME="My App"
Special charactersQuote and escapeDB_PASS="p@ss#w0rd!"
CommentsLines starting with ## This is a comment
NumbersNo quotes neededPORT=3000
Booleanstrue or false (string)DEBUG=true
Empty valueLeave value blankOPTIONAL_VAR=

Loading .env in Different Platforms

# Node.js (dotenv)
npm install dotenv
// require("dotenv").config() at top of app

# Docker Compose
env_file:
  - .env

# Python (python-dotenv)
pip install python-dotenv
from dotenv import load_dotenv; load_dotenv()

# GitHub Actions
env:
  MY_VAR: ${{ secrets.MY_VAR }}

How to Convert JSON to .env Format

Converting JSON to .env format is a common task when moving configuration between secrets managers, CI/CD pipelines, and local development environments. Follow these steps to convert JSON to .env correctly every time.

  1. Start with a flat or shallow JSON object. The .env format stores only scalar values (strings, numbers, booleans). Deeply nested JSON must be flattened first using underscore separators.
  2. Uppercase all key names. Environment variable names are conventionally written in UPPER_SNAKE_CASE. The key database.host becomes DATABASE_HOST.
  3. Replace dots and hyphens with underscores. Characters like . and - are not valid in environment variable names in most shells. Replace them with _.
  4. Quote values that contain spaces or special characters. Wrap such values in double quotes: APP_NAME="My App".
  5. Omit null and array values, or convert them. Arrays can be stored as comma-separated strings if your application knows how to parse them back.

Step-by-Step JSON to .env Conversion Example

// Step 1: Original nested JSON (from AWS Parameter Store, config file, etc.)
{
  "database": {
    "host": "db.example.com",
    "port": 5432,
    "name": "production_db",
    "password": "s3cur3p@ss!"
  },
  "app": {
    "name": "My API Service",
    "debug": false,
    "allowed_origins": ["https://app.example.com", "https://admin.example.com"]
  },
  "stripe_secret_key": "sk_live_abc123"
}

// Step 2: Flatten nested keys with _ separator, uppercase all keys
// Step 3: Arrays → comma-separated strings; quote values with spaces/special chars

# .env output
DATABASE_HOST=db.example.com
DATABASE_PORT=5432
DATABASE_NAME=production_db
DATABASE_PASSWORD="s3cur3p@ss!"
APP_NAME="My API Service"
APP_DEBUG=false
APP_ALLOWED_ORIGINS="https://app.example.com,https://admin.example.com"
STRIPE_SECRET_KEY=sk_live_abc123

JSON to ENV vs ENV to JSON

Both conversion directions are useful in different situations. JSON to .env is common when importing configuration from cloud services; .env to JSON is useful when you need to pass environment configuration to tools that expect a JSON input.

Aspect JSON to .env .env to JSON
Common use caseImport from secrets manager, Vault, AWS SSM, or config APIExport env vars for Terraform, Docker Compose, or JSON-based config tools
Nesting supportNested JSON is flattened to KEY_SUBKEY formatFlat env vars are converted to top-level JSON keys only
Array handlingArrays stored as comma-separated stringsString values kept as strings; no automatic array reconstruction
Key casingJSON keys uppercased → UPPER_SNAKE_CASEEnv var names kept as-is or converted to camelCase
Lossless round-tripNot always — arrays and nested objects may lose structurePossible for flat scalar values; nesting not reconstructed automatically
Best toolThis JSON to .env converterdotenv-cli, env2json npm package, or a custom script

Common .env File Patterns

Most .env files follow predictable naming conventions grouped by service or concern. Here are the most common patterns you will encounter in real projects:

Database Configuration

# PostgreSQL / MySQL
DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DB_USER=appuser
DB_PASSWORD=secret

# Redis
REDIS_URL=redis://localhost:6379
REDIS_HOST=localhost
REDIS_PORT=6379

Application Settings

# Node.js / Express
NODE_ENV=production
PORT=3000
APP_SECRET=your-jwt-secret-here
CORS_ORIGIN=https://app.example.com
LOG_LEVEL=warn

# Next.js / React
NEXT_PUBLIC_API_URL=https://api.example.com
NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX

Third-Party API Keys

# Payment
STRIPE_SECRET_KEY=sk_live_...
STRIPE_PUBLISHABLE_KEY=pk_live_...
STRIPE_WEBHOOK_SECRET=whsec_...

# Email
SENDGRID_API_KEY=SG.xxxxx
SMTP_HOST=smtp.sendgrid.net
SMTP_PORT=587
SMTP_USER=apikey
SMTP_PASS=SG.xxxxx

# Storage
AWS_ACCESS_KEY_ID=AKIA...
AWS_SECRET_ACCESS_KEY=xxxxx
AWS_REGION=us-east-1
S3_BUCKET=my-app-uploads

Feature Flags

# Feature toggles
FEATURE_NEW_DASHBOARD=true
FEATURE_BETA_API=false
MAINTENANCE_MODE=false
MAX_UPLOAD_SIZE_MB=50

Explore more tools: All JSON Tools | Validator | Pretty Print | JSON Diff