Skip to content

Instantly share code, notes, and snippets.

@brouberol
Created February 27, 2013 08:00
Show Gist options
  • Save brouberol/5046094 to your computer and use it in GitHub Desktop.
Save brouberol/5046094 to your computer and use it in GitHub Desktop.
If you try to insert a JSON structure into MongoDB, and some of its keys contain a "." (dot), insertion will fail (see http://docs.mongodb.org/manual/reference/limits/#Restrictions%20on%20Database%20Names) You'll need to replace the dot by something else to make insertion possible.
# Regex finding all dots in all keys of a JSON structure
DOT_IN_DICT_KEY = re.compile(r"""[,{] # opening bracket for first keys, or previous comma
\s* # spaces until first key quote
['"]([^.'"]+\.[^'"]+)['"] # key name in bewteen quotes
\s*:""", # : after key name ,
flags=re.UNICODE | re.VERBOSE)
def dotrepl(matchobj):
"""Replace all . by _ in the match"""
return matchobj.group().replace('.', '_')
def clean_google_result(json_struct):
""" Sanitize google API results.
Some dict keys contain a "." character, which prevents
insertion into MongoDB.
"""
fixed_json_struct = re.sub(DOT_IN_DICT_KEY, dotrepl,
json.dumps(json_struct))
return json.loads(fixed_json_struct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment