Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not escape backslash when writing to JSON

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?

like image 746
yesh Avatar asked Oct 25 '25 00:10

yesh


1 Answers

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
like image 95
Akaisteph7 Avatar answered Oct 27 '25 14:10

Akaisteph7