Reputation: 115
Given a json file which contains lots of arrays/objects, and the variable _json
equals the following:
{
"response":{
"data":[
{
"a":"Foo",
"b":"Bar",
"name":"\\\\""John Doe\\\\""",
"gender":"male"
}
]
}
}
When I run json.loads(_json)['response']['data']
, python raises the exception:
json.decoder.JSONDecodeError: Expecting ',' delimiter
Placing r
before the json string doesn't work, as it appears to be that json.loads
still chokes on escapes since the error happens around here "John Doe\\\\"""
. There may also be cases with more backslashes.
Is there a way to remove these backslashes? Maybe with regex?
I would highly appreciate any feedback. Thanks!
Upvotes: 1
Views: 276
Reputation:
The string is not valid json. Fix it upstream. If you want to patch it up here, then this should do the trick:
json.loads(_json.replace('\\\\"',''))['response']['data']
Upvotes: 1