On the host, I have an environment variable with a quote sign in the value, something like:
export VALUE_WITH_QUOTE_FROM_OS='quote"value'
and when I echo on bash, it's fine
#echo $VALUE_WITH_QUOTE_FROM_OS
quote"value
I have a following json string:
json_str = '{"key":"${VALUE_WITH_QUOTE_FROM_OS}"}'
Then I want to expand the environment variable inside a python script before further processing, something like this
json_str = os.path.expandvars(json_str)
json_dict = json.loads(json_str)
However, this expansion would break the json syntax, since the json_str had become
'{"key":"quote"value"}' (<== bad unescaped quote in the value)
instead of
'{"key":"quote\"value"}'
Is there anyway I can inform os.path.expandvars() to escape the double quote when expanding the value? If not, how should I expand the environment variable so that the double quote can be escaped.
Note 1 The value of the environment variable is a security token, so I have to keep the double quote there as it is.
Note 2 Current json interface has already been determined and used extensiely as it is. This json_str is passed to me, therefore I should and must only expand the environment variable denoted by ${} in the json string, no other modification are allowed.
Note 3 This json_str is extremely large with complex, dynamic, nested structure and is consumed by multiple clients who cannot access host os environment variable. Although it's possible for me to load the json_str first, traverse the dictionary to resolve the environment variable, and then dump the dict back to json_str and then dispatch to all the clients, I think it is less efficient compared to processing it just as a string.
Thanks.