Created
June 17, 2014 11:38
-
-
Save Mause/34b6def69418bea10fd3 to your computer and use it in GitHub Desktop.
This file contains 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
def clean_json(google_json): | |
# delete anti-xss junk ")]}'\n" (5 chars); | |
google_json = google_json[4:] | |
# pass through result and turn empty elements into nulls | |
instring = False | |
inescape = False | |
lastchar = '' | |
normal_json = "" | |
for char in google_json: | |
# toss unnecessary whitespace | |
if char.isspace() or not char: | |
continue | |
# handle strings | |
if instring: | |
if inescape: | |
normal_json += char | |
inescape = False | |
elif char == '\\': | |
normal_json += char | |
inescape = True | |
elif char == '"': | |
normal_json += char | |
instring = False | |
else: | |
normal_json += char | |
lastchar = char | |
continue | |
if char == '"': | |
normal_json += char | |
instring = True | |
elif char == ',': | |
if lastchar in [',', '[', '{']: | |
normal_json += 'null' | |
normal_json += char | |
elif char in ['}', ']']: | |
if lastchar == ',': | |
normal_json += 'null' | |
normal_json += char | |
else: | |
normal_json += char | |
lastchar = char | |
return normal_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment