How to iterate through an OrderedDict contained in another object generated by Zeep?

348 views Asked by At

I've used Zeep to call a SOAP API and return an object whose ._dict_ attributes list looks like this:

{'__values__': OrderedDict([('Ids', {
    'Id': [
        {
            'Name': 'ID Name One',
            'Value': '00192'
        },
        {
            'Name': 'ID Name Two',
            'Value': '999996'
        },
        {
            'Name': 'ID Name 3',
            'Value': 'GF'
        },
        {
            'Name': 'ID Four',
            'Value': 'AA'
        },
        {
            'Name': 'ID Name 5',
            'Value': 'Fund Name'
        },
        {
            'Name': 'Account Code',
            'Value': 'FCOM102'
        },
        {
            'Name': 'Allocation Code',
            'Value': '00192'
        }
    ]
}), ('ID', 284), ('Amount', Decimal('25.0000')), ('Text', 'Name of my Organization'), ('Value', '0'), ('Instructions', '')])}

My current solution is to use Zeep Helpers to serialize the object, and iterate through it using .items() with a for loop and a bunch of if statements(see below), but surely there has to be a more elegant way to tackle this iteration?

z = zeep.helpers.serialize_object(b.MyObject) # Use the Zeep helper module to convert the zeep array to an OrderedDict
           for k, v in z.items(): #  Begin iterating through converted object
               if isinstance (v, OrderedDict): # The key named Ids has an OrderedDict for a Value, I'm detecting it like this.
                   for i in v.items(): # We begin itereting through this OrderedDict (Ids)
                       h = 0
                       while h < len(i[1]): # i is a tuple containing the string 'Id' followed by more OrderedDicts
                           for id_n, id_v in i[1][h].items(): # Iterating through the tuple's OrderedDicts
                             print(id_n + ' : ' + id_v + 'i[1][h].items()')
                           h+=1 # Increment to next tuple value
                       #print(i[1][0].items().Value)
               else:
                   print(k + ' : ' + str(v) + ' | z.items() not ordict')
0

There are 0 answers