Skip to content

Instantly share code, notes, and snippets.

@alexrutherford
Last active September 22, 2015 00:34
Show Gist options
  • Save alexrutherford/a578aedbdfcd0c950659 to your computer and use it in GitHub Desktop.
Save alexrutherford/a578aedbdfcd0c950659 to your computer and use it in GitHub Desktop.
Python function to recursively clean $ from JSON to place in MongoDB
#################
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
@alexrutherford
Copy link
Author

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'

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