The answer by @Michael gave me an idea... not a very pretty idea, but it seems to work, at least on your example: Try to parse the JSON string, and if it fails, look for the character where it failed in the exception string1 and replace that character.
while True:
try:
result = json.loads(s) # try to parse...
break # parsing worked -> exit loop
except Exception as e:
# "Expecting , delimiter: line 34 column 54 (char 1158)"
# position of unexpected character after '"'
unexp = int(re.findall(r'(char (d+))', str(e))[0])
# position of unescaped '"' before that
unesc = s.rfind(r'"', 0, unexp)
s = s[:unesc] + r'"' + s[unesc+1:]
# position of correspondig closing '"' (+2 for inserted '')
closg = s.find(r'"', unesc + 2)
s = s[:closg] + r'"' + s[closg+1:]
print result
You may want to add some additional checks to prevent this from ending in an infinite loop (e.g., at max as many repetitions as there are characters in the string). Also, this will still not work if an incorrect "
is actually followed by a comma, as pointed out by @gnibbler.
Update: This seems to work pretty well now (though still not perfect), even if the unescaped "
is followed by a comma, or closing bracket, as in this case it will likely get a complaint about a syntax error after that (expected property name, etc.) and trace back to the last "
. It also automatically escapes the corresponding closing "
(assuming there is one).
1) The exception's str
is "Expecting , delimiter: line XXX column YYY (char ZZZ)"
, where ZZZ is the position in the string where the error occurred. Note, though, that this message may depend on the version of Python, the json
module, the OS, or the locale, and thus this solution may have to be adapted accordingly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…