Question: 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?

Answer: 
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 want to represent `-----BEGINKEY-----\nVgIBMIIE` in JSON, it has to go in a double quoted string.

In double quoted strings, backslashes have special meaning. It's not possible to write a backslash character simply by typing `"\"`. This would generate an error.

Therefore, the backslash must be escaped. Hence `-----BEGINKEY-----\nVgIBMIIE` becomes `"-----BEGINKEY-----\\nVgIBMIIE"` in a JSON string.

If you want to get rid of the backslash, you need to represent

```
-----BEGINKEY-----
VgIBMIIE

```

rather than

```
-----BEGINKEY-----\nVgIBMIIE

```

These two things are not the same thing.