sfactor sfactor - 1 month ago 20
JSON Question

Python: How to escape double quote inside json string value?

I'm having trouble parsing a json string in python because there are extra double quotes inside the string values like

{"name": "Jack O"Sullivan", "id": "1"}


I'm trying to convert it into list for further evaluation like so:

import ast
js = '{"name": "Jack O"Sullivan", "id": "1"}'
ast.literal_eval(js).values()


How do I change the json string to be something like this
"Jack O\'Sullivan"
, so that it evaluated properly.

Edit Just to stress that I know the json is invalid but this is what I've got and changing the source is NOT an option. I'm looking to work around this limitation at the moment.

Answer
import re

json = '{"name": "Jack O"Sullivan", "id": "1"}'

fixed = re.sub(r'("[\s\w]*)"([\s\w]*")',r"\1\'\2", json)

I suspect this will work (working example at repl.it), it uses the following regex:

("[\s\w]*)"([\s\w]*")

and then replacing any inner " with \'. This will work as long as the inclusion list is valid (the [\s\w]), ie valid strings will only include spaces and word characters. You may have to add additional possibilities for more complex names.

It matches any string "<alpha/space>"<alpha/space>" and then replaces it with "<whatwasbefore>\'<whatwasafter>" using capture groups and back references.

See the example at regex101