I am making calls to a SOAP API using suds, which returns data as objects rather than raw XML. I want to save a copy of the raw response in addition to what I parse, with the end goal being storage as JSON (I'm currently using TinyDB for testing).
The overall flow looks like this:
- Retrieve raw response
- Create a dict of the raw response using the script below
- Parse response objects for later use
- Serialize everything to JSON and store in TinyDB
My script for converting a suds object to a dict is:
def makeDict(response):
out = {}
for k, v in asdict(response).iteritems():
if hasattr(v, '__keylist__'):
out[k] = makeDict(v)
elif isinstance(v, list):
out[k] = []
for item in v:
if hasattr(item, '__keylist__'):
out[k].append(makeDict(item))
else:
out[k].append(item)
else:
out[k] = v
return out
However, sometimes when I run makeDict(object)
and try to serialize to JSON, I get a type error like the following:
File "C:\Python27\Lib\json\encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: (Date){
Day = 7
Month = 8
Year = 2004
} is not JSON serializable
This error is throwing me off because:
- I know this object
Date
appears in other records that don't throw an error during serialization - type(Date.Day) is an int and the field name is a string
Does anyone have an idea as to what's going on here? It looks as if it's trying to serialize the raw object, but everything I'm inserting into TinyDB has already been run through makeDict