Home Web Front-end JS Tutorial From JSON to Environment Variables: Introducing json-to-env-converter

From JSON to Environment Variables: Introducing json-to-env-converter

Jan 02, 2025 pm 05:54 PM

From JSON to Environment Variables: Introducing json-to-env-converter

Hi there! This is my first-ever article (so go easy on me in the comments ?). I’m excited to share a little project I’ve been working on which came out of a personal need. If you’ve ever wrestled with managing configurations in your Node.js application, you might find this small zero dependency package interesting. It’s called json-to-env-converter.

What is json-to-env-converter?

json-to-env-converter is an npm package that converts JSON objects into environment variables. It’s a lightweight tool designed to help you handle JSON-based configurations by converting them into environment variables and injecting it into process.env; it's intended for scenarios where configurations might be dynamic, nested, or sourced from APIs or external systems.

Here’s the idea: Instead of manually setting environment variables for complex or dynamic configurations, you can programmatically load them from a JSON object and access them just like any other environment variable.

Why Did I Build It?

I've recently built a secrets api and for every project that I'm personally working on; I'm calling my secrets api to get my config at runtime which is provided in json format. Also not to mention in modern and large apps config can change depending on the user location, or other dynamic factors. While .env files are great for static setups, they don’t easily handle:

  • Nested structures: Flattening nested JSON into environment variables can get tedious.
  • Dynamic sources: Loading configurations at runtime without restarting the app isn’t straightforward.

I built json-to-env-converter to explore a way to address these issues once again it was more of project for personal use and it’s definitely not meant to replace tools like dotenv, but rather to handle a slightly different use case; and I thought what's the harm in making it open source and publishing it publicly on npm and I would be so happy if anyone found use of it.

How It Works

Install the package from npm:

npm i json-to-env-converter
Copy after login
Copy after login

Here’s a simple example to show what it does:

Basic Example

import { jsonToEnv } from 'json-to-env-converter';

const config = {
  database: {
    host: 'localhost',
    port: 5432,
  },
};

jsonToEnv(config);

console.log(process.env.DATABASE_HOST); //Output: 'localhost'
console.log(process.env.DATABASE_PORT); //Output: '5432'
Copy after login
Copy after login

This takes a JSON object and converts it into environment variables. It also flattens nested keys, so database.host becomes DATABASE_HOST.

Adding a Prefix

To avoid collisions, you can add a prefix:

jsonToEnv(config, { prefix: 'MYAPP_' });

console.log(process.env.MYAPP_DATABASE_HOST); //Output: 'localhost'
Copy after login

A Use Case: Dynamic Configurations

One potential use case for this package is handling dynamic configurations. For example, imagine you have a global application that fetches region-specific settings at runtime. Instead of manually managing .env files for each region, you could dynamically load the right settings based on the user’s location:

npm i json-to-env-converter
Copy after login
Copy after login

This allows your app to adapt its configuration without requiring a restart or hardcoded values.

Should You Use It?

Honestly, I’m still deciding on how broadly useful this package might be. If you’re already comfortable with .env files and static configs, you might not need this tool. But if you’re working with:

  • Dynamic environments where configs change at runtime
  • Nested JSON objects that need to be converted into flat environment variables
  • Programmatic configuration setups sourced from APIs or external services

and not happy with your current setup then json-to-env-converter might save you some time and worth a try.

Installation and Feedback

If you’re curious to give it a try, you can install it from npm:

import { jsonToEnv } from 'json-to-env-converter';

const config = {
  database: {
    host: 'localhost',
    port: 5432,
  },
};

jsonToEnv(config);

console.log(process.env.DATABASE_HOST); //Output: 'localhost'
console.log(process.env.DATABASE_PORT); //Output: '5432'
Copy after login
Copy after login

I’d love to hear your thoughts, feedback, or most importantly suggestions for improvement so feel free to mention in the comments here or submit pull requests on my github repo https://github.com/neenus/json-to-env. This is a learning experience for me, and I’m excited to see where it goes.

Oh and one more thing... thanks for reading my first article!

The above is the detailed content of From JSON to Environment Variables: Introducing json-to-env-converter. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles