Skip to content

Instantly share code, notes, and snippets.

@shyjuzz
Created February 11, 2020 07:28
Show Gist options
  • Save shyjuzz/85beeae1fc91ebd42640f77dfd4338f3 to your computer and use it in GitHub Desktop.
Save shyjuzz/85beeae1fc91ebd42640f77dfd4338f3 to your computer and use it in GitHub Desktop.
convert python object recursively to dict
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