Python-zeep: getting python objects (list, dict) instead of zeep.objects in the result?

Created on 14 Jun 2016  路  18Comments  路  Source: mvantellingen/python-zeep

Hello

Is it possible to get the results as python types like dict and list instead of zeep.objects ?
Or is there an easy way to transform them?

Thanks

Most helpful comment

All 18 comments

@mvantellingen great, I did not see the helpers module. Thanks
Any way to get dict objects instead of OrderedDict?

So far, the only way I see is using json conversion

input_dict = helpers.serialize_object(zeep_object)
output_dict = json.loads(json.dumps(input_dict))

It would be nice to specify the type of dict we want in the serializer function though.

Is there any reason you don't want an ordereddict? There are ofcourse better ways then a roundtriip through json encode/decode :)

On a second thought, I think it's fine. It actually makes sense to get an OrderedDict when the XSD defines sequences.
Having the option to specify the default dict type in the serializer might still be valuable, though.

Anyway, zeep looks promising so far, good job. :+1:
It's hard to find a modern soap client in python. Thanks for making zeep.

Thanks you. helper.serialize_object does the job. zeep rocks. Thank you @mvantellingen and your team. I'm using it to build a client to call Bronto APIs. Very clean very nice. Thanks.

oh my lawd, how did I not see the helper module I've been flying blind for over a year now

@mvantellingen I think I can see how serialize_object would be useful to create a dict from an object so we could bulk update the dict from another dict, but how best to get it back into the original zeep.object?

@mvantellingen I think I can see how serialize_object would be useful to create a dict from an object so we could bulk update the dict from another dict, but how best to get it back into the original zeep.object?

Ah, I think I answered my own question: https://python-zeep.readthedocs.io/en/master/datastructures.html

However instead of creating an object from a type defined in the XSD you can also pass in a dictionary. Zeep will automatically convert this dict to the required object (and nested child objects) during the call.

Good!

I know this issue was closed in the past, but using the above code posted by @YAmikep, I get the following output (this is on Python 3.6.9/zeep==3.4.0):

def some_func(self, id)
    zeep_object = self._client.service.SomeOperation(id)
    input_dict = helpers.serialize_object(zeep_object)
    print(input_dict)
    output_dict = json.loads(json.dumps(input_dict))
    return output_dict

<server id="11111" name="ServerA" state="In" modeGroup="A" manualMode="false" clusterName="ClusterA" 
clusterId="222" connections="2" environment="ENVA" platform="PLATA" vip="11.11.111.111">
<serverInstance name="ServerA-01" ip="11.11.111.111" vip="22.22.222.222" actualState="In Service" expectedState="In 
Service" connections="2" lastUpdated="2020-07-07 13:34:49" />
</server>

('<server id="11111" name="ServerA" state="In" modeGroup="A" '
'manualMode="false" clusterName="ClusterA" clusterId="222" connections="2" '
'environment="ENVA" platform="PLATA" vip="11.11.111.111">\n'
'<serverInstance name="ServerA-01" ip="11.11.111.111" '
'vip="22.22.222.222" actualState="In Service" expectedState="In Service" '
'connections="2" lastUpdated="2020-07-07 13:34:49" />\n'
'</server>\n')

I've been able to get around this so far by using xmltodict.parse(), but it would be great to eliminate an external dep if at all possible. Any and all help is appreciated, thank you.

I know this issue was closed in the past, but using the above code posted by @YAmikep, I get the following output (this is on Python 3.6.9/zeep==3.4.0):

def some_func(self, id)
    zeep_object = self._client.service.SomeOperation(id)
    input_dict = helpers.serialize_object(zeep_object)
    print(input_dict)
    output_dict = json.loads(json.dumps(input_dict))
    return output_dict

<server id="11111" name="ServerA" state="In" modeGroup="A" manualMode="false" clusterName="ClusterA" 
clusterId="222" connections="2" environment="ENVA" platform="PLATA" vip="11.11.111.111">
<serverInstance name="ServerA-01" ip="11.11.111.111" vip="22.22.222.222" actualState="In Service" expectedState="In 
Service" connections="2" lastUpdated="2020-07-07 13:34:49" />
</server>

('<server id="11111" name="ServerA" state="In" modeGroup="A" '
'manualMode="false" clusterName="ClusterA" clusterId="222" connections="2" '
'environment="ENVA" platform="PLATA" vip="11.11.111.111">\n'
'<serverInstance name="ServerA-01" ip="11.11.111.111" '
'vip="22.22.222.222" actualState="In Service" expectedState="In Service" '
'connections="2" lastUpdated="2020-07-07 13:34:49" />\n'
'</server>\n')

I've been able to get around this so far by using xmltodict.parse(), but it would be great to eliminate an external dep if at all possible. Any and all help is appreciated, thank you.

@beardedeagle I'm not sure what the motivation is to for the json.loads/json.dumps, but if you're just trying to get a python dict type out of serialize_object you can specify the type you want.

input_dict = helpers.serialize_object(zeep_object, dict)

Saved my day, thanks guys!

input_dict = helpers.serialize_object(zeep_object, dict)

I import helper from zeep and include this line after I get the response back from zeep, but its not working. I get a string of xml in a 'ResultObject' within the zeep response. I tried using the zeep response and the resultobject within it as the zeep_object but neither changes after the helper runs.

input_dict = helpers.serialize_object(zeep_object, dict)

I import helper from zeep and include this line after I get the response back from zeep, but its not working. I get a string of xml in a 'ResultObject' within the zeep response. I tried using the zeep response and the resultobject within it as the zeep_object but neither changes after the helper runs.

Is there anyway you could share the zeep response object you're trying to serialize?

{ 'ResultObject': '<?xml version="1.0" encoding="utf-8"?><TransactionsV5 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><InvoicePaymentInfo><PaymentInfo><InvoiceNumber>111116</InvoiceNumber><TotalAmount xsi:nil="true" /><CustomerName>Kuroash Esmaili</CustomerName><TaxAmount xsi:nil="true" /><Status>PAID</Status><PaymentDate>2021-03-09T03:00:00</PaymentDate><AccountNumber>N/A</AccountNumber><Balance>0</Balance><PaymentType>MANUALLY</PaymentType><Discount>0</Discount><CustomerHolder /><ReferenceNumber /><AuthNumber /><TraceNumber /><PaymentStatus>APPROVED</PaymentStatus><GrossAmount xsi:nil="true" /><NetAmount xsi:nil="true" /><UserId>infortest</UserId><ProductType>NA</ProductType><PaymentAmount>37.3</PaymentAmount><PaymentTaxAmount>0</PaymentTaxAmount><CustomerPickupTime /><InvoiceTotalAmount>37.3</InvoiceTotalAmount><InvoiceTaxAmount>2.3</InvoiceTaxAmount></PaymentInfo></InvoicePaymentInfo></TransactionsV5>', 'LogMessage': None, 'Message': 'Operation successful', 'MessageId': 0, 'Succeed': True }

@kesmaili-ag so you're trying to have zeep serialize an XML string to a python dictionary? I don't think it's going to do that for you, you'd probably want to look at something like xmltodict

Not sure if this belongs here but this link gave me a cleaner result than xmltodict for turning a XML string to a dict. https://stackoverflow.com/questions/2148119/how-to-convert-an-xml-string-to-a-dictionary/5807028#5807028?newreg=2ba5f5a0145a419fbf4b07c8d5b688ec

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dhinchakdhoom picture dhinchakdhoom  路  4Comments

grguthrie picture grguthrie  路  4Comments

ryan-mccaffrey picture ryan-mccaffrey  路  5Comments

jacksonwoody picture jacksonwoody  路  5Comments

eagle-r picture eagle-r  路  6Comments