pythonsoapdictionarysuds

Parsing Suds SOAP complex data type into Python dict


I have some data coming from a SOAP API using Suds which I need to parse in my Python script. Before I go off and write a parser (there is more than just this one to do):

1) Does anyone recognise what this is? It's the standard complex object datatype as returned by Suds (documentation). Should have spotted that.

2) If so, is there an existing library that I can use to convert it to a Python dictionary? How do I parse this object into a Python dict? It seems I can pass a dictionary to Suds but can't see an easy way of getting one back out.

(ArrayOfBalance){
   Balance[] = 
      (Balance){
         Amount = 0.0
         Currency = "EUR"
      },
      (Balance){
         Amount = 0.0
         Currency = "USD"
      },
      (Balance){
         Amount = 0.0
         Currency = "GBP"
      },
 }

Solution

  • There is a class method called dict in suds.client.Client class which takes a sudsobject as input and returns a Python dict as output. Check it out here: Official Suds Documentation

    The resulting snippet becomes as elegant as this:

    from suds.client import Client
    
    # Code to obtain your suds_object here...
    
    required_dict = Client.dict(suds_object)
    

    You might also want to check out items class method (link) in the same class which extracts items from suds_object similar to items method on dict.