Skip to content

Instantly share code, notes, and snippets.

@imosquera
Created October 3, 2012 22:06
Show Gist options
  • Select an option

  • Save imosquera/3830191 to your computer and use it in GitHub Desktop.

Select an option

Save imosquera/3830191 to your computer and use it in GitHub Desktop.
def to_flat_dict(json_obj, flat_keyname="", flat_dict = {}):
#recursive function that turns the json dict into key/values pairs
format = "%s.%s"
if flat_keyname == "": format = "%s%s"
if isinstance(json_obj,dict):
for (k, json_obj) in json_obj.items():
new_flatname = format % (flat_keyname,k)
flat_dict = to_flat_dict(json_obj,new_flatname, flat_dict)
elif isinstance(json_obj,list):
for obj in json_obj:
flat_dict = to_flat_dict(obj, flat_keyname, flat_dict)
else:
if flat_keyname in valid_keys:
flat_dict[flat_keyname] = json_obj
return flat_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment