如何在Python中获得特定级别的JSON?

时间:2018-12-18 07:14:53

标签: python json hierarchical-data

如果我的JSON数据如下所示:

{
    "name": "root",
    "children": [
        {
            "name": "a",
            "children": [
                {
                    "name": "b",
                    "children": [
                        {
                            "name": "c",
                            "size": "1"
                        },
                        {
                            "name": "d",
                            "size": "2"
                        }
                    ]
                },
                {
                    "name": "e",
                    "size": 3
                }
            ]
        },
        {
            "name": "f",
            "children": [
                {
                    "name": "g",
                    "children": [
                        {
                            "name": "h",
                            "size": "1"
                        },
                        {
                            "name": "i",
                            "size": "2"
                        }
                    ]
                },
                {
                    "name": "j",
                    "size": 5
                }
            ]
        }
    ]
}

如何在Python中返回两个相邻的级别?

例如返回:
a-b,e
f-g,j

数据可能会变得非常大,因此我必须将其切成小块。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

尝试此解决方案,告诉我是否可行。

dictVar = {
    "name": "root",
    "children": [
        {
            "name": "a",
            "children": [
                {
                    "name": "b",
                    "children": [
                        {
                            "name": "c",
                            "size": "1"
                        },
                        {
                            "name": "d",
                            "size": "2"
                        }
                    ]
                },
                {
                    "name": "e",
                    "size": 3
                }
            ]
        },
        {
            "name": "f",
            "children": [
                {
                    "name": "g",
                    "children": [
                        {
                            "name": "h",
                            "size": "1"
                        },
                        {
                            "name": "i",
                            "size": "2"
                        }
                    ]
                },
                {
                    "name": "j",
                    "size": 5
                }
            ]
        }
    ]
}

name = {}
for dobj in dictVar['children']:
    for c in dobj['children']:
        if not dobj['name'] in name:
            name[dobj['name']] = [c['name']]
        else:
            name[dobj['name']].append(c['name'])
print(name)

并且由于您需要所有原始数据,所以另一个是:

name = {}
for dobj in dictVar['children']:
    for c in dobj['children']:
        if not dobj['name'] in name:
            name[dobj['name']] = [c]
        else:
            name[dobj['name']].append(c)
print(name)

答案 1 :(得分:0)

您需要构建一棵dict的树,其值作为叶子:

{'a': {'b': {'c': '1', 'd': '2'}, 'e': '3'}, 'f': {'g': {'h': '1', 'i': '2'}, 'j': '5'}}

这可以分解为三个单独的动作:

  1. 获取节点的"name"作为密钥
  2. 如果节点具有"children",请将其转换为dict
  3. 如果节点具有"size",请将其转换为单个值

除非您的数据深度嵌套,否则递归是一种简单的方法:

def compress(node: dict) -> dict:
    name = node['name']  # get the name
    try:
        children = node['children']  # get the children...
    except KeyError:
        return {name: node['size']}  # or return name and value
    else:
        data = {}
        for child in children:       # collect and compress all children
            data.update(compress(child))
        return {name: data}

这将压缩整个层次结构,包括"root"节点:

 >>> compress(data)
 {'root': {'a': {'b': {'c': '1', 'd': '2'}, 'e': 3},
           'f': {'g': {'h': '1', 'i': '2'}, 'j': 5}}}