Fix very bad JSON
examples/regex/very_bad.json
[ { TID : "t-0_login_sucess" Test : [ {SetValue : { uname : "Zorg", pass : "Rules"} }, {DoAction : "login"}, {CheckResult: [0, LOGGED_IN]} ] }, { TID : "t-1_login_failure", Test : [ {SetValue : { uname : "11", pass : "im2happy78"} }, {DoAction : "login"}, {CheckResult: [-1000, LOGGED_OUT]} ] } ]
examples/regex/fix_very_bad_json.py
import re, json, os json_file = os.path.join( os.path.dirname(__file__), 'very_bad.json' ) with open(json_file, 'r') as fh: bad_json = fh.read() #print(bad_json) improved_json = re.sub(r'"\s*$', '",', bad_json, flags=re.MULTILINE) #print(improved_json) # good_json = re.sub(r'(?<!")(?P<word>[\w-]+)\b(?!")', '"\g<word>"', # improved_json) # good_json = re.sub(r'(?<[\{\s])(?P<word>[\w-]+)(?=[:\s])', '"\g<word>"', # improved_json) # good_json = re.sub(r'([\{\[\s])(?P<word>[\w-]+)([:,\]\s])', '\1"\g<word>"\3', # improved_json) good_json = re.sub(r'(?<=[\{\[\s])(?P<word>[\w-]+)(?=[:,\]\s])', '"\g<word>"', improved_json) #print(good_json) # with open('out.js', 'w') as fh: # fh.write(good_json) data = json.loads(good_json) print(data)