Deno.js Tutorial to Convert JSON File to .env File Using TypeScript Full Project For Beginners

 

 

JSON to Dotenv

Use this Deno script to parse a json file and output a .env file. I have also included an equivalent Node.js script json-to-dotenv.js for comparison.

Usage:

deno json_to_dotenv.ts path/to/file.json path/to/.env

Example JSON Input:

{
  "key": "string",
  "someKey": 1234,
  "anotherKey": true,
  "nullKey": null,
  "parentKey": {
    "key": "string",
    "someKey": 1234,
    "anotherKey": true,
    "nullKey": null
  },
  "arrayKey": [
    "value",
    "someValue",
    "anotherValue"
  ]
}

Example Dotenv Output:

KEY=string
SOMEKEY=1234
ANOTHERKEY=true
NULLKEY=null
PARENTKEY_KEY=string
PARENTKEY_SOMEKEY=1234
PARENTKEY_ANOTHERKEY=true
PARENTKEY_NULLKEY=null
ARRAYKEY=value,someValue,anotherValue

 

 

json_to_dotenv.ts

 

 

import { resolve } from "https://deno.land/std/fs/path.ts";
import { readFileStr, writeFileStr } from "https://deno.land/std/fs/mod.ts";

type JSONValue = null | string | number | boolean | IJSONObject;

interface IJSONObject {
  [key: string]: JSONValue | JSONValue[] | IJSONObject | IJSONObject[];
}

async function main() {
  const jsonFilePath = Deno.args[1];
  const outputFilePath = Deno.args[2] || '.env';

  if (!jsonFilePath) {
    console.error(
      "Please provide a path to a json file to parse\n\nExample: deno json_to_dotenv.ts path/to/file.json"
    );
    return;
  }

  let jsonString = "";
  try {
    jsonString = await readFileStr(resolve(jsonFilePath));
  } catch (e) {
    console.error(`Unable to read file: ${jsonFilePath}`);
    console.error(e);
    return;
  }

  let configObject: IJSONObject = {};
  try {
    configObject = JSON.parse(jsonString);
  } catch (e) {
    console.error(`Unable to parse json from file: ${jsonFilePath}`);
    console.error(e);
    return;
  }

  const keyValuePairs = parseKeyValuePairs(configObject);
  const output = keyValuePairs.map(formatKeyValuePair).join("\n");

  await writeFileStr(resolve(outputFilePath), output + "\n");
}

function parseKeyValuePairs(json: IJSONObject, prefix?: string) {
  const keyValuePairs = Object.keys(json).reduce<[string, string][]>((pairs, key) => {
    const value = json[key];

    const displayKey = (prefix ? `${prefix}_${key}` : key).toUpperCase();

    if (
      value === null ||
      typeof value === "string" ||
      typeof value === "number" ||
      typeof value === "boolean"
    ) {
      pairs.push([displayKey, `${value}`]);
    } else if (Array.isArray(value)) {
      pairs.push([displayKey, value.join(",")]);
    } else if (typeof value === "object") {
      pairs = pairs.concat(
        parseKeyValuePairs(value, displayKey)
      );
    }

    return pairs;
  }, []);

  return keyValuePairs;
}

function formatKeyValuePair(kayValuePair: [string, string]) {
  return `${kayValuePair[0]}=${kayValuePair[1]}`;
}

main();

Leave a Reply