python-zeep icon indicating copy to clipboard operation
python-zeep copied to clipboard

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

Open YAmikep opened this issue 10 years ago • 18 comments

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

YAmikep avatar Jun 14 '16 19:06 YAmikep

See http://docs.python-zeep.org/en/latest/helpers.html

mvantellingen avatar Jun 14 '16 19:06 mvantellingen

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

YAmikep avatar Jun 14 '16 20:06 YAmikep

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.

YAmikep avatar Jun 14 '16 20:06 YAmikep

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

mvantellingen avatar Jun 14 '16 20:06 mvantellingen

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.

YAmikep avatar Jun 14 '16 21:06 YAmikep

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.

dlink avatar May 19 '17 16:05 dlink

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

beautah avatar Dec 13 '17 21:12 beautah

@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?

DougPlumley avatar Jul 15 '19 14:07 DougPlumley

@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.

DougPlumley avatar Jul 15 '19 14:07 DougPlumley

Good!

rabelorp avatar May 07 '20 19:05 rabelorp

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 avatar Jul 07 '20 21:07 beardedeagle

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)

DougPlumley avatar Jul 17 '20 15:07 DougPlumley

Saved my day, thanks guys!

aparnabushan-zz avatar Aug 19 '20 06:08 aparnabushan-zz

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.

kesmaili-ag avatar Sep 16 '20 19:09 kesmaili-ag

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?

DougPlumley avatar Sep 16 '20 20:09 DougPlumley

{ '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 avatar Sep 16 '20 20:09 kesmaili-ag

@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

DougPlumley avatar Sep 16 '20 20:09 DougPlumley

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

kesmaili-ag avatar Sep 16 '20 22:09 kesmaili-ag