Skip to content

Instantly share code, notes, and snippets.

@Mause
Created June 17, 2014 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mause/34b6def69418bea10fd3 to your computer and use it in GitHub Desktop.
Save Mause/34b6def69418bea10fd3 to your computer and use it in GitHub Desktop.
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