stackbuilders / dotenv-hs

Load environment variables from dotenv files for Haskell
https://hackage.haskell.org/package/dotenv
MIT License
65 stars 14 forks source link

How to provide JSON as a value to the env var #169

Closed Martinsos closed 1 year ago

Martinsos commented 1 year ago

Hi, do you have any advice how to provide JSON as a value for the env var?

For example, let's say I want to do something like this:

MY_VAR={"apiKey":"key"}

Currently this throws parse error at the first ", saying that " was unexpected and it expected "$(", "${", '$', end of input, end of line, escaped character, or unescaped character.

I guess " are not allowed in the value? Is this also what other dotenv libraries do, don't allow " in the value? If so, is there a way to go around this somehow?

Thanks a lot!

CristhianMotoche commented 1 year ago

Hi @Martinsos Thank you so much for using dotenv! Have you tried to wrap the value with single quotes?

# .env
JSON='{"a":[1,2,3]}'

I tested locally and it seems to be working fine:

$ dotenv 'echo $JSON' | jq .a
[
  1,
  2,
  3
]
Martinsos commented 1 year ago

That works perfect! Thanks a lot! Might be worth adding in the docs? What if we have both single and double quotes in our string -> what could we do then?

CristhianMotoche commented 1 year ago

Hey @Martinsos You can always escape the inner quotes with \' or \" depending on the quotes that surround it:

Surround with ''

# .env
JSON='{"a":[1,2,3], "b": "\'asdf\'"}'

Surrounding with ""

# .env
JSON="{\"a\":[1,2,3], \"b\": \"'asdf'\"}"
CristhianMotoche commented 1 year ago

I'll include a section about this in the README! Thanks for the suggestion. :smile:

Martinsos commented 1 year ago

So even that works -> ok that is amazing, thanks a lot for being so proactive and helpful!