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?