如何将我的JSON转换为制作D3旭日形图所需的格式?

时间:2017-01-03 16:24:11

标签: python json

我有以下JSON数据:

{
    "data": {
        "databis": {
            "dataexit": {
                "databis2": {
                    "1250": { }
                }
            },
            "datanode": {
                "20544": { }
            }
        }
    }
}

我想用它来生成D3 sunburst diagram,但这需要different data format

{
    "name": "data",
    "children": [ 
        {
            "name": "databis",
            "children": [
                {
                    "name": "dataexit",
                    "children": [
                        {
                            "name": "databis2",
                            "size": "1250"
                        }
                    ]
                },
                {
                    "name": "datanode",
                    "size": "20544"
                }
            ]
        }
    ]
}

我怎样才能用Python做到这一点?我想我需要使用递归函数,但我不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

您可以使用带有名称和字典作为参数的函数的递归解决方案。对于给定dict中的每个项目,它再次调用自身以生成如下所示的子项列表:{'name': 'name here', 'children': []}

然后它将检查特殊情况,其中只有一个孩子的密钥children,其值为空列表。在这种情况下,dict返回了参数作为名称,子名称作为大小。在所有其他情况下,函数返回dict的名称和子项。

import json

data = {
    "data": {
        "databis": {
            "dataexit": {
                "databis2": {
                    "1250": { }
                }
            },
            "datanode": {
                "20544": { }
            }
        }
    }
}

def helper(name, d):
    # Collect all children
    children = [helper(*x) for x in d.items()]

    # Return dict containing size in case only child looks like this:
    # {'name': '1250', 'children': []}
    # Note that get is used to so that cases where child already has size
    # instead of children work correctly
    if len(children) == 1 and children[0].get('children') == []:
        return {'name': name, 'size': children[0]['name']}

    # Normal case where returned dict has children
    return {'name': name, 'children': [helper(*x) for x in d.items()]}

def transform(d):
    return helper(*next(iter(d.items())))

print(json.dumps(transform(data), indent=4))

输出:

{
    "name": "data",
    "children": [
        {
            "name": "databis",
            "children": [
                {
                    "name": "dataexit",
                    "children": [
                        {
                            "name": "databis2",
                            "size": "1250"
                        }
                    ]
                },
                {
                    "name": "datanode",
                    "size": "20544"
                }
            ]
        }
    ]
}