Last active
September 22, 2015 00:34
-
-
Save alexrutherford/a578aedbdfcd0c950659 to your computer and use it in GitHub Desktop.
Python function to recursively clean $ from JSON to place in MongoDB
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 sanitiseNames(dummyObject): | |
################# | |
if type(dummyObject)==dict: | |
for k,v in dummyObject.items(): | |
if re.search('\$',k,re.U): | |
# Replace | |
dummyObject[k.replace('$','')]=v | |
del dummyObject[k] | |
dummyObject[k.replace('$','')]=sanitiseNames(v) | |
# Clean those values | |
return dummyObject | |
elif type(dummyObject)==list: | |
# Might be list of dictionaries | |
for e in dummyObject: | |
dummyObject=[sanitiseNames(e) for e in dummyObject] | |
return dummyObject | |
else: | |
# If bottom out to int or string | |
return dummyObject | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist to clean a JSON object so it can be placed in MongoDB. Any keys containing '$' cannot be handled in MongoDB, therefore replace keys such as '$t' as returned by YouTube API with 't'