amane
amane

Reputation: 39

how to remove 'escape slash' in json?

I need to remove escape sequence. How can I do it using python?

"conv": "\"XXXXX\"",
"conv": "XXXXX",

Upvotes: 3

Views: 12604

Answers (4)

hgb123
hgb123

Reputation: 14891

Use replace

conv = "\"XXXXX\""
conv = conv.replace("\"", "")

Upvotes: -1

windstorm
windstorm

Reputation: 385

You can use json decoder

import json
x = {"conv": "\"XXXXX\""}
x['conv'] = json.loads(x['conv']) # sets conv to "XXXXX"

Upvotes: 0

Darkborderman
Darkborderman

Reputation: 139

You can remove string using str.replace method.

'\"XXXXX\"'.replace("\"","") # returns 'XXXXX'

Here's how to use str.replace()

Upvotes: -1

Booboo
Booboo

Reputation: 44283

You need to double-up on the back-slashes, otherwise you will not end up with something that will parse as json because otherwise you are just replacing all " characters and leaving the backslash:

s = s.replace('\\"', '')
import json


d = {"conv": "\"XXXXX\""}
s = json.dumps(d)
print(s) # -> {"conv": "\"XXXXX\""}
s = s.replace('\\"', '')
print(s) # -> {"conv": "XXXXX"}
print(json.loads(s)) # -> {'conv': 'XXXXX'}

Upvotes: 0

Related Questions