user13581602
user13581602

Reputation: 115

How to remove malformatted escaped backslashes from json?

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

Answers (1)

user9706
user9706

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

Related Questions