-1

I use zeep to call a webservice.

response = proces.service.Load(**params2, _soapheaders={'Header': header_value})

This returns an object which looks like this

{
    'LoadResult': None,
    'hierarchy': {     
        'Code': 'FTE', 
        'Name': 'Balans en Winst & verlies',
        'Description': None,
        'RootNode': {
            'Id': 757,
            'Code': 'FTE',
            'Name': 'Balans en Winst & verlies',
            'Description': None,
            'Accounts': None,
            'ChildNodes': {
                'HierarchyNode': [
                    {
                        'Id': 758,
                        'Code': '000',
                        'Name': 'Immateriële vaste activa',
                        'Description': None,
                        'Accounts': None,
                        'ChildNodes': {
                            'HierarchyNode': [
                                {
                                    'Id': 759,
                                    'Code': '00010',
                                    'Name': 'Goodwill',
                                    'Description': None,
                                    'Accounts': {
                                        'HierarchyAccount': [
                                            {
                                                'Type': 'BAS',
                                                'Code': '0100',
                                                'BalanceType': 'Balance'
                                            },
                                            {
                                                'Type': 'BAS',
                                                'Code': '0105',
                                                'BalanceType': 'Balance'
                                            }
                                        ]
                                    },
                                    'ChildNodes': None,
                                    'Messages': None,
                                    'Touched': 173
                                }
                            ]
                        },
                        'Messages': None,
                        'Touched': 173
                    },
                    {
                        'Id': 760,
                        'Code': '010',
                        'Name': 'Materiële vaste activa',
                        'Description': None,
                        'Accounts': None,
                        'ChildNodes': {
                            'HierarchyNode': [
                                {
                                    'Id': 761,
                                    'Code': '01010',
                                    'Name': 'Bedrijfsgebouwen en -terreinen',
                                    'Description': None,
                                    'Accounts': {
                                        'HierarchyAccount': [
                                            {
                                                'Type': 'BAS',
                                                'Code': '0090',
                                                'BalanceType': 'Balance'
                                            },
                                            {
                                                'Type': 'BAS',
                                                'Code': '0110',
                                                'BalanceType': 'Balance'
                                            },
                                            {
                                                'Type': 'BAS',
                                                'Code': '0115',
                                                'BalanceType': 'Balance'
                                            },
                                            {
                                                'Type': 'BAS',
                                                'Code': '0120',
                                                'BalanceType': 'Balance'
                                            },
                                            {
                                                'Type': 'BAS',
                                                'Code': '0125',
                                                'BalanceType': 'Balance'
                                            }
                                        ]
                                    },
                                    'ChildNodes': None,
                                    'Messages': None,
                                    'Touched': 173
                                },
                                {
                                    'Id': 762,
                                    'Code': '01020',
                                    'Name': 'Machines en installaties',
                                    'Description': None,
                                    'Accounts': {

etc.

I want this hierarchy for a report. I want the resulting table to look something like

enter image description here

So from left to right starting at the lowest hierarchy.

How can I best achieve this? It is not just a straightforward json response. It says it cannot be serialized.

enter image description here

and

enter image description here

I read that the JSON should not have single quotes. When I try to correct that:

enter image description here

Jsonlint says this, when pasting the string corrected with double quotes

enter image description here

  • 2
    `response` has already been parsed into a Python object. You don't need to parse it again. – Barmar Dec 17 '20 at 23:58
  • When I do type(response) it says Class – Rubbervuist Dec 18 '20 at 00:15
  • You're able to access nested data using things like `response.hierarchy.Code`. Why do you think you need to convert it from JSON? – Barmar Dec 18 '20 at 00:17
  • Hmm I guess because every google result about python object to table speaks of JSON data. I'm not sure how to proceed otherwise – Rubbervuist Dec 18 '20 at 00:23
  • Just access all the attributes the same way. `response.hierarchy.RootNode.ChildNodes.HierarchyNode` – Barmar Dec 18 '20 at 00:26
  • Could you perhaps point me a little further in the right direction? for elem in response['hierarchy']['RootNode']: for ouder in ['ChildNodes']: for item in ['HierarchyNode']: for child in ['ChildNodes']: for a in ['HierarchyNode']: for c in ['Accounts']: for d in ['HierarchyAccount']: print(d[0]) This is what I'm doing now but it just gives me single letters – Rubbervuist Dec 18 '20 at 21:52

2 Answers2

0

The JSON has already been parsed by the API library, and it constructed a hierarchy of objects that can be accessed using attributes. The class also apparently provides its own __repr__() method that makes it look like a hierarchy of dictionaries; but it's not actually dictionaries, so you can't use ['Attribute'] syntax.

If you want to loop through the HierarchyNode list you could use

for node in response.hierarchy.RootNode.ChildNodes.HierarchyNode:
    print(node.Id, node.Name, node.Code)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hey Barmar, I got it to work thanks to your comment. I'll update my starting post to reflect the code. Thanks for sticking with me. – Rubbervuist Dec 18 '20 at 22:36
  • You shouldn't put the solution in the question. If you have a more complete solution than mine, post it as an answer. – Barmar Dec 18 '20 at 22:38
0

Got it to work using

        for node in response.hierarchy.RootNode.ChildNodes.HierarchyNode:
            if(hasattr(node.ChildNodes, 'HierarchyNode')):
                for nood in node.ChildNodes.HierarchyNode:
                    if(hasattr(nood.Accounts, 'HierarchyAccount')):
                        for noodje in nood.Accounts.HierarchyAccount:
                            print(noodje.Code, noodje.Type,node.Id,node.Name,nood.Name, nood.Code)