Created
April 8, 2015 13:42
-
-
Save robcowie/a6a56cf5b17a86fdf461 to your computer and use it in GitHub Desktop.
Serialising Suds objects to json
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
from suds.sudsobject import asdict | |
def recursive_asdict(d): | |
"""Convert Suds object into serializable format.""" | |
out = {} | |
for k, v in asdict(d).iteritems(): | |
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): | |
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