Ask coding questions

← Back to all posts
How can I use a .env in a JSON file?
Ipex (1)

I already have a .env file with my key in it, but right now I just need to be able to access it through the JSON file.

How can I do that?

Comments
hotnewtop
Coder100 (18057)

Hello, assuming you are coding in node.js, there is a very sneaky way to do it. First, do JSON.stringify() on the JSON file. For example:

const json = require("./path/to/json.json")
console.log(JSON.stringify(json));

Now, copy the output, and put it inside your .env file:

JSON=[insert json here]

Now, anytime you want to access it,

const output = JSON.parse(process.env.JSON)

Hopes this helps!

SpicedSpices (305)

If you are talking about this repl:

https://repl.it/@Ipex/MA-Cafe-Bot#bot.js

then all you have to do is do something in your javascript code such as:

let secret = process.env.SECRET

and in your .env file
SECRET: abjoiuegouuefk90j

You can put that secret through the JSON file from your javascript

coderash (286)

what llanguage are u using with json? if python:
os.getenv(KEY)