0

I have a Unicode string my_string = 'SGtjPQ\\u003d\\u003d' and a dictionary (2 backslashes)

data = {'key': my_string}

I need to provide json-response in bytes, so I do the following

response = json.dumps(data)
return response.encode()

and eventually a have this result b'{"key": "SGtjPQ\\\\u003d\\\\u003d"}' (4 backslashes). But I want my_string in the response to be exactly as it was (with 2 backslashes). How to prevent this auto-escaping and get result b'{"key": "SGtjPQ\\u003d\\u003d"}'

3
  • What are you trying to do with these two backslashes in front of u00 ? The normal syntax for unicode characters uses only one slash. Commented Jul 30, 2015 at 15:52
  • So what, you want your JSON result to represent the string "SGtjPQ\u003d\u003d"‽ Commented Jul 30, 2015 at 15:53
  • This behavior isn't necessary in real life but if you still need to achieve that result you can do response.replace(b'\\\\', b'\\') to explicitly replace that slashes. Commented Jul 31, 2015 at 8:44

2 Answers 2

1

I'm not sure why you decided to put two backslashes in your python string in front of the code u003d. The syntax to specify unicode characters using their numeric code uses only one slash. Like this: "SGtjPQ\u003d\u003d"

Now, to answer your question about why you get the 4 slashes in the displayed string. This inflation of the number of backslashes is normal:

my_string in memory -> only one slash

representation of my_string using Python syntax for strings -> two backslashes

representation of my_string in json -> two backslashes

representation (using Python syntax for strings) of the json representation of my_string -> 4 backslashes

So you have 4 backslashes in the end result because the slash is first escaped by the JSON encoding, and then, the two resulting slashes are escaped by the display of the python interpreter, which displays strings using the Python syntax for strings.

Sign up to request clarification or add additional context in comments.

Comments

1

A backslash inside a JSON string is an escape character and itself needs to be escaped. "\\" inside JSON means a single backslash. What you get, the four backslashes, is the correct JSON syntax for representing two backslashes.

If you want it any differently, you should get your string in order before you JSON encode it. Don't write escaped JSON literal syntax, write the characters that you mean and let JSON encode it correctly for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.