-
-
Save Arkhangel/7db62aaf096847871008e9a75793442b to your computer and use it in GitHub Desktop.
Serialising Suds objects to json (Python3)
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
from suds.sudsobject import asdict | |
def recursive_asdict(d): | |
"""Convert Suds object into serializable format.""" | |
out = {} | |
for k, v in asdict(d).items(): | |
if hasattr(v, '__keylist__'): | |
out[k] = recursive_asdict(v) | |
elif isinstance(v, list): | |
out[k] = [] | |
for item in v: | |
if hasattr(item, '__keylist__'): | |
out[k].append(recursive_asdict(item)) | |
else: | |
out[k].append(item) | |
else: | |
out[k] = v | |
return out | |
def suds_to_json(data): | |
"""Convert suds object to json""" | |
return json.dumps(recursive_asdict(data)) | |
raw = a_suds_response | |
data = [recursive_asdict(t) for t in raw] | |
out = json.dumps(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment