6

I'm getting a result from a SOAP API like that:

client = zeep.Client(wsdl=self.wsdl, transport=transport)
auth_header = lb.E("authenticate", self.login())
res  = client.service.GetHouseProfile(region_id, page_number, reporting_period_id, _soapheaders=[auth_header])

now I need to parse res and to get a result.

>>> dir(res)
['__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__values__', '__weakref__', '_xsd_type']
>>> type(res)
<class 'zeep.objects.GetHouseProfileSFResponse'>
>>> print(res.__str__()[0:100])
{
    'data': {
        'item': [
            {
                'house_id': 6465882L,

How to get a certain element from res?

So I found the way. Looks like not a standard decision but it works:

>>> res.__values__.get("data").__values__.get("item")[6].__values__.keys()
[u'house_id', u'house_profile_data', u'full_address', u'stage', u'state', u'emergency_date', u'emergency_number', u'emergency_reason', u'emergency_after', u'inn', u'files_info']
Rainmaker
  • 10,294
  • 9
  • 54
  • 89

2 Answers2

8
import zeep
#import json

a = zeep.helpers.serialize_object(res)

#json_object_a = json.loads(json.dumps(a))

print a['data']['item'][0]['house_id']
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
  • a = ast.literal_eval(r) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/ast.py", line 49, in literal_eval node_or_string = parse(node_or_string, mode='eval') File "/usr/lib/python2.7/ast.py", line 37, in parse return compile(source, filename, mode, PyCF_ONLY_AST) File "", line 361 'create_date': datetime.datetime(2015, 4, 6, 19, 15, 26, tzinfo=) ^ SyntaxError: invalid syntax – Rainmaker Dec 26 '16 at 06:36
  • add 1 more import on top line and try again. `from datetime import datetime` – Mohammad Yusuf Dec 26 '16 at 06:39
  • >>> from datetime import datetime >>> a = ast.literal_eval(r) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/ast.py", line 49, in literal_eval node_or_string = parse(node_or_string, mode='eval') File "/usr/lib/python2.7/ast.py", line 37, in parse return compile(source, filename, mode, PyCF_ONLY_AST) File "", line 361 'create_date': datetime.datetime(2015, 4, 6, 19, 15, 26, tzinfo=) ^ SyntaxError: invalid syntax – Rainmaker Dec 26 '16 at 06:41
  • check if `res` object has something like `res.to_json()` or `res.body()` method. Check by this `print dir(res)` – Mohammad Yusuf Dec 26 '16 at 06:48
  • @Rainmaker Oops. missed it. – Mohammad Yusuf Dec 26 '16 at 06:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131474/discussion-between-mygz-and-rainmaker). – Mohammad Yusuf Dec 26 '16 at 06:56
  • @Rainmaker Try now – Mohammad Yusuf Dec 26 '16 at 07:00
6

The __values__ attribute is a private implementation detail and you shouldn't really use it. You should be able to just do response.data.item[0].house_id or response['data']['item'][0]['house_id'].

See https://github.com/mvantellingen/python-zeep/blob/master/src/zeep/xsd/valueobjects.py#L32 for the code used.

Cheers, Michael (author of zeep)

mvantellingen
  • 1,229
  • 10
  • 6
  • Michael, is this an answer to my question? https://stackoverflow.com/questions/46203751/how-to-get-and-set-ref-or-out-parameters-in-python-wsdl-soap-client – pashute Sep 18 '17 at 05:44
  • Hi Michael. Thanks so much for zeep. have a look please at this related question here...https://github.com/mvantellingen/python-zeep/issues/571 – pashute Oct 23 '17 at 12:01