Home > Enterprise >  python3 how to set os.environ variable when its value is json object?
python3 how to set os.environ variable when its value is json object?

Time:11-06

Have tried,

export "INDEX_SET"="{"index_all":false,"index_group":true,"index_channel":true,"exclude_chats":[],"include_chats":[1522205730]}"

but received this error:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Please set the INDEX_SETTINGS environment variable correctly

from the python code as:

index_set_str = os.environ["INDEX_SET"].strip()
index_settings = json.loads(index_settings_str)

CodePudding user response:

Your shell quoting is wrong. Try:

export INDEX_SET='{"index_all":false,"index_group":true,"index_channel":true,"exclude_chats":[],"include_chats":[1522205730]}'

CodePudding user response:

You need to escape the double quotes when exporting the environment variable. For example:

export INDEX_SET="{\"index_all\":false,\"index_group\":true,\"index_channel\":true,\"exclude_chats\":[],\"include_chats\":[1522205730]}"

Alternatively, export it in Base64 encoding and decode it in your code.

  • Related