Dealing with mis-escaped characters in JSON
Question: I am reading a JSON file into Python which contains escaped single quotes
(_\'_). This leads to all kinds of hiccups, as nicely discussed e.g.
[here](http://stackoverflow.com/questions/2275359/jquery-single-quote-in-json-
response). However, I could not find anything on how to **address** the issue.
I just did a

    
    
    newstring=originalstring.replace(r"\'", "'")
    

and things worked out. But this seems rather ugly. I could not really find
much material on how to deal with this kind of thing (creating an exception,
or something) in the json [docs](https://docs.python.org/2/library/json.html)
either.

  * Is there a good, clean procedure for such an issue?

Going back to the source is not possible, unfortunately.

Thanks for your help!


Answer: The right thing would be to fix whatever is creating the invalid JSON file.
But if that's not possible, I guess the replace is needed. But you should use
a regular expression so it doesn't replace `\\'` with `\'` \-- in this case
the first backslash is escaping the second backslash, they're not escaping the
quote. A negative lookbehind will prevent this.

    
    
    import re
    newstring = re.sub(r"(?<!\\)\\'", "'", originalstring)
    

