Xerobit

JSON to ENV Converter

This json to env tool converts a JSON object into flat KEY=value lines suitable for .env files. Nested objects are flattened using an underscore separator, arrays are serialized as JSON strings, and all values are quoted. Paste your input on the left and copy the result. It runs entirely in your browser, so nothing is uploaded to any server.

Result

DB_HOST="localhost"
PORT="5432"
  • Variables
    2
  • Prefix
    (none)
  • Separator
    underscore

How to use the json to env converter

  1. Paste a valid JSON object into the JSON input text area.
  2. Optionally add a key prefix such as APP_ to namespace every variable.
  3. Choose a nested key separator: single or double underscore.
  4. The output updates live. Copy the generated lines directly into your .env file.

How the conversion works

The converter parses the input with JSON.parse and walks the resulting object tree. For each leaf value it builds a key by joining ancestor property names with the chosen separator, converting everything to uppercase. The value is then stringified and wrapped in double quotes.

Consider this input: {"database":{"host":"localhost","port":5432}}. The walker visits database.host and database.port, producing DATABASE_HOST="localhost" and DATABASE_PORT="5432".

Keys are sorted alphabetically at every level so the output order is deterministic regardless of how the original JSON was formatted.

Handling special values

Environment variables are always strings, so non-string types need conversion. Numbers become their decimal representation. Booleans become the literal words true or false. A JSON null becomes an empty string between the quotes.

Arrays and deeply nested objects that you do not want flattened can be kept as JSON strings. When the walker encounters an array, it serializes the entire array with JSON.stringify and places the result inside quotes. This means your application code will need to parse that value back from JSON at runtime.

Unicode characters are preserved exactly as they appear. If your JSON contains an emoji, the output line will contain the actual emoji character inside the quotes.

Escaping rules

Because every value is enclosed in double quotes, certain characters inside the value must be escaped so that parsers read them correctly. The converter applies these escape sequences:

CharacterEscaped form
Backslash (\)\\
Double quote (")\"
Newline\n
Carriage return\r
Tab\t

This matches the escaping behaviour expected by popular libraries such as dotenv for Node.js and Python's python-dotenv.

Common mistakes to avoid

A frequent error when performing a json to env conversion is pasting a JSON array instead of an object. The root value must be an object because environment variables require named keys. If you have an array, wrap it in an object first, for example {"ITEMS":[...]}.

Another issue is forgetting that key names are uppercased automatically. If your downstream system expects mixed-case variable names, you will need to adjust the output manually. Most operating systems treat environment variable names as case-sensitive on Linux and macOS but case-insensitive on Windows, so uppercase is the safest convention.

If you need to transform JSON into other structured formats rather than flat variables, tools like json to xml or json to sql may be more appropriate.

Using the output in your project

Copy the generated lines into a file named .env at the root of your project. Make sure this file is listed in your .gitignore so secrets are not committed to version control.

In Node.js, install the dotenv package and call require('dotenv').config() at the entry point of your application. In Python, use python-dotenv and call load_dotenv(). Docker Compose reads .env files automatically when you use the env_file directive.

For generating language-specific type definitions from the same JSON source, see sibling converters such as json to go struct, json to c# class, json to java class, or json to python class.

Worked examples

ExampleInputResult
Database configJSON input: {"DB":{"HOST":"localhost","PORT":5432},"DEBUG":true}; Nested key separator: _Variables: 3; Prefix: (none); Separator: underscore
With prefixJSON input: {"API_KEY":"abc123","TIMEOUT":30}; Key prefix (optional): MYAPP_; Nested key separator: _Variables: 2; Prefix: MYAPP_; Separator: underscore
Arrays and nullsJSON input: {"TAGS":["web","api"],"DESCRIPTION":null,"COUNT":0}; Nested key separator: _Variables: 3; Prefix: (none); Separator: underscore

FAQ

What happens if my JSON is invalid?

The converter uses the built-in JSON.parse function. If the input is malformed, it displays the exact parser error message so you can fix the syntax. You can also run it through a json fixer first.

Can I convert a JSON array to .env format?

Not directly. The root value must be a JSON object because environment variables require named keys. Wrap your array in an object, for example {"DATA":[1,2,3]}, and the array will be stored as a JSON string value.

Are the values quoted in the output?

Yes. Every value is wrapped in double quotes with internal quotes, backslashes, and newlines escaped. This ensures compatibility with standard .env parsers.

Does this tool upload my data anywhere?

No. The json to env conversion runs entirely in your browser using JavaScript. Nothing is sent to any server.

How are nested objects handled?

Nested objects are flattened by joining the key path with the separator you choose, typically an underscore. All key segments are converted to uppercase.

What about null and boolean values?

Null becomes an empty string. Booleans become the lowercase words true or false, enclosed in quotes.