Created
October 3, 2012 22:06
-
-
Save imosquera/3830191 to your computer and use it in GitHub Desktop.
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
| 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