Skip to content

Instantly share code, notes, and snippets.

@hatarist
Last active August 9, 2018 21:09
Show Gist options
  • Save hatarist/ba010e8937e213df5528e0d52188ab8d to your computer and use it in GitHub Desktop.
Save hatarist/ba010e8937e213df5528e0d52188ab8d to your computer and use it in GitHub Desktop.
this 100% mature and tested function strips C-style comments and trailing commas from a (not-so-valid) JSON
import re
def clean_json(payload):
# remove C-style comments
payload = re.sub(re.compile("/\*.*?\*/",re.DOTALL), '', payload)
payload = re.sub(re.compile("//.*?\n" ), '', payload)
# remove trailing commas
payload = re.sub(re.compile(r',\s*?([\]\}])'), r'\1', payload)
return payload
@hatarist
Copy link
Author

hatarist commented Aug 9, 2018

In case you found this shit piece of code and missed the sarcasm bit of the "100% mature and tested" phrase above:
Make sure you fucking know what you're doing before you cluelessly use this to solve your problem.
Otherwise you'll end up wasting hours trying to determine why does your precious mega complex json have some fucked-up data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment