Python Backslash JSON Escaping issue - adding unwanted double backslash \\

65 Views Asked by At

For a Locust performance test, I have some JSON that I need to send to an API. I need to randomise some of the data in the JSON before sending it but annoyingly the JSON has some escaped double quotes that are scuppering things as JSON.loads is adding an extra \ to escape the backslash. Is there a way to stop this?

Setup - simplified for the sake of the question:

  1. I have 0.json with content like this:
{
    "id": 1,
    "question": "Do you prefer ${option_a} or ${option_b}",
    "info": "here is some <b class=\"indented-content\">extra info</b>"
}
  1. I have a dict of replacement text:
replacement_data = dict(
    option_a="bacon",
    option_b="eggs"
)
  1. I then use the following to read in the JSON, replace the items as needed and load it as json ready to send to the API
from string import Template
def generate_data():
    file = open('0.json')
    file_str = Template(file.read())
    data = json.loads(file_str.substitute(replacement_data))
return data

The problem is that if I print the resulting data to debug it, it looks like this:

{
    "id": 1,
    "question": "Do you prefer bacon or eggs",
    "info": "here is some <b class=\\"indented-content\\">extra info</b>"
}

i.e. it's escaped the backslashes. This then gives me a 400 error when sending to the API as it is not formatted correctly.

I know it's unusual to have this situation, but is there a way to get it to replace the data and load it but not add in the extra backslashes please?

I have tried reading in the file after and doing a find and replace on double \ to , but this didn't work.

I've also tried changing the JSON to unicode, but that didn't work either... i.e.

{
    "id": 1,
    "question": "Do you prefer ${option_a} or ${option_b}",
    "info": "here is some <b class=\u00sc\u0022indented-content\u005c\u0022>extra info</b>"
}

Info I know that proper JSON should not contain the backslash and that's why it gets removed. I agree with the many comments saying this, but that's not helpful to my question as if it's not there, the server rejects the file.

I didn't code the server side, I am merely trying to automate some tests on it so please don't shoot the messenger.

If anyone know how to get it so that the 2 individual single backslashes are preserved, please let me know (NB They are used to escape the " on the server side, so if they are missing, it fails).

0

There are 0 best solutions below