SOAPpy result to JSON

458 views Asked by At

I am sending a request to an external WDSL service, which for some reason returns a huge object. I am connecting using SOAPpy, and I would like to parse the result to JSON. However I am getting a lot of objects within the object, like this.

<SOAPpy.Types.structType incident at 53129728>: 

Is there any way I can parse this object with SOAPpy? Is my first time using this SOAPpy.

3

There are 3 answers

0
user3730927 On

This is what worked for me:

pythonObject = SOAPpy.Types.simplify(soapObject)

jsonObject = json.dumps(pythonObject)

0
aestartus On

i had that same problem, i found a solution here:

https://github.com/paultag/deapi/blob/master/deapi/emitters.py

basically you have create a Encoder json class and give to dump the class for encoding.

something like this:

class DateEncoder(json.JSONEncoder):

def default(self, obj):
    if isinstance(obj, date):
        return str(obj)
    if isinstance(obj, structType):
        obj = obj._asdict()
        return obj
    return json.JSONEncoder.default(self, obj)

and them:

response = json.dumps(response,sort_keys=True, indent=4, cls=DateEncoder)
0
kikanga On

If you are using SOAPpy - there is a method that converts the SOAPpy object to a native Python object - which then drops in to json.dumps without error.

soappyobject = SOAPProxy.someMethod()
pythonobject = SOAPProxy.Types.simplify(soapyobject)
jsonobject = json.dumps(pythonobject)

...at least - its worked on every object I dropped in to it so far.

Unfortunately it doesn't work on a WSDL object that SOAPpy.WSDL can return.