The problem is that JSON doesn't support the \xNN
escape, only \uNNNN
. Replacing \x22
with "
will make the JSON invalid. What you should do is to replace \xNN
with \u00NN
:
>>> q = q.replace('\\x', '\\u00')
>>> json.loads(q)
{'request_body': '{"username":"2","password":"6","id":"e2cad174-736e-3041-cf7e", "FName":"HS"}', 'request_method': 'POST'}
(Note: This is not a complete solution as it will erratically replace correctly-escaped strings like abc\\xyz
to abc\\u00yz
. A complete solution probably needs a regex or even a lexer.)