Created
February 11, 2020 07:28
-
-
Save shyjuzz/85beeae1fc91ebd42640f77dfd4338f3 to your computer and use it in GitHub Desktop.
convert python object recursively to dict
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 obj_to_dict(obj): | |
if type(obj) is dict: | |
res = {} | |
for k, v in obj.items(): | |
res[k] = obj_to_dict(v) | |
return res | |
elif type(obj) is list: | |
return [obj_to_dict(item) for item in obj] | |
elif type(obj) is SimpleNamespace: | |
return obj_to_dict(vars(obj)) | |
else: | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment