I am trying to read a JSON file and add a new key,value pair to it. Then write the JSON file back. Example
# Read the JSON and add a new K,V
input_file_path = os.path.join(os.path.dirname(__file__), 'test.json')
input_file = open(input_file_path, 'r')
data = json.load(input_file)
data['key'] = os.environ.get('KEY', '') #<-- Adding a new K,V
input_file.close()
# Write the JSON to tmp.
output_file = open('/tmp/' + 'test.json', 'w')
json.dump(data, output_file, indent=4)
output_file.close()
My input JSON looks like this
{
"type": "account"
}
My env variable called KEY looks like this
-----BEGINKEY-----\nVgIBMIIE
The final JSON file written to tmp looks like this
{
"private_key": "-----BEGINKEY-----\\nVgIBMIIE",
"type": "account"
}
I am not able to figure out why the backslash is escaped? How can I avoid this?
The program is treating your input string as a raw string and so adds the extra \. The original '\' is not actually escaping anything, so for it to be represented in Python as a string, you need to escape it. As you saw this can be problematic however. You can force the string back to unicode format using:
import codecs
raw_val = os.environ.get('KEY', '')
val = codecs.decode(raw_val, 'unicode_escape')
data['key'] = val
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With