Last active
March 1, 2024 12:50
-
-
Save bryaneaton/c8d69aa57e9e0f97b6c65142408866c3 to your computer and use it in GitHub Desktop.
Fix Broken JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import re | |
with open("somefile.json", 'r', encoding='utf-8) as f: | |
s = f.read() | |
# Original code here by tobias_k: https://stackoverflow.com/questions/18514910/how-do-i-automatically-fix-an-invalid-json-string | |
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:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment