Last active
August 9, 2018 21:09
-
-
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
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.