<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Neenus Gabriel</title>
    <description>The latest articles on DEV Community by Neenus Gabriel (@neenus).</description>
    <link>https://dev.to/neenus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F247924%2F188b543a-05bc-487f-b46c-6884251ae701.jpeg</url>
      <title>DEV Community: Neenus Gabriel</title>
      <link>https://dev.to/neenus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/neenus"/>
    <language>en</language>
    <item>
      <title>From JSON to Environment Variables: Introducing json-to-env-converter</title>
      <dc:creator>Neenus Gabriel</dc:creator>
      <pubDate>Tue, 24 Dec 2024 05:25:31 +0000</pubDate>
      <link>https://dev.to/neenus/from-json-to-environment-variables-introducing-json-to-env-converter-1k25</link>
      <guid>https://dev.to/neenus/from-json-to-environment-variables-introducing-json-to-env-converter-1k25</guid>
      <description>&lt;p&gt;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 &lt;a href="https://www.npmjs.com/package/json-to-env-converter" rel="noopener noreferrer"&gt;json-to-env-converter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is json-to-env-converter?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Did I Build It?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nested structures: Flattening nested JSON into environment variables can get tedious.&lt;/li&gt;
&lt;li&gt;Dynamic sources: Loading configurations at runtime without restarting the app isn’t straightforward.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install the package from npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i json-to-env-converter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s a simple example to show what it does:&lt;/p&gt;

&lt;p&gt;Basic Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;jsonToEnv&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;json-to-env-converter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;jsonToEnv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_HOST&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Output: 'localhost'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DATABASE_PORT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Output: '5432'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This takes a JSON object and converts it into environment variables. It also flattens nested keys, so database.host becomes DATABASE_HOST.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding a Prefix&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To avoid collisions, you can add a prefix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;jsonToEnv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MYAPP_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MYAPP_DATABASE_HOST&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//Output: 'localhost'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A Use Case: Dynamic Configurations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;regionConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;us&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;dbHost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us-db.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.us.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;eu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;dbHost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;eu-db.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.eu.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;region&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;us&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This could come from a runtime check&lt;/span&gt;
&lt;span class="nf"&gt;jsonToEnv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regionConfig&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;region&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;APP_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;APP_DBHOST&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 'us-db.example.com'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows your app to adapt its configuration without requiring a restart or hardcoded values.&lt;/p&gt;

&lt;p&gt;**Available Options&lt;br&gt;
The &lt;code&gt;jsonToEnv&lt;/code&gt; function accepts an optional options object with the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prefix&lt;/code&gt;: A string to add a prefix to the environment variable names (default: &lt;code&gt;''&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;envFile&lt;/code&gt;: A boolean to enable or disable the creation of the &lt;code&gt;.env&lt;/code&gt; file (default: &lt;code&gt;false&lt;/code&gt;); if enabled and the file already exists, it will be overwritten.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;envFileName&lt;/code&gt;: A string to specify the name of the &lt;code&gt;.env&lt;/code&gt; file (default: &lt;code&gt;.env&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;envFilePath&lt;/code&gt;: A string to specify the path where the &lt;code&gt;.env&lt;/code&gt; file should be created (default: &lt;code&gt;.&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Should You Use It?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic environments where configs change at runtime&lt;/li&gt;
&lt;li&gt;Nested JSON objects that need to be converted into flat environment variables&lt;/li&gt;
&lt;li&gt;Programmatic configuration setups sourced from APIs or external services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and not happy with your current setup then json-to-env-converter might save you some time and worth a try.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation and Feedback&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’re curious to give it a try, you can install it from npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i json-to-env-converter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;a href="https://github.com/neenus/json-to-env" rel="noopener noreferrer"&gt;https://github.com/neenus/json-to-env&lt;/a&gt;. This is a learning experience for me, and I’m excited to see where it goes.&lt;/p&gt;

&lt;p&gt;Oh and one more thing... thanks for reading my first article!&lt;/p&gt;

</description>
      <category>json</category>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
