简体   繁体   中英

Parsing a nested JSON(?) object in Python

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

在此处输入图像描述

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.

在此处输入图像描述

and

在此处输入图像描述

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

在此处输入图像描述

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

在此处输入图像描述

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM