如何使用python向JSON子代添加唯一ID?

时间:2018-08-02 20:14:18

标签: python json helper id

我有一个包含许多子代的json文件,如下所示:

{
    "tree": {
        "name": "Top Level",
        "children": [
            {
                "name": "[('server', 'Cheese')]",
                "children": [
                    {
                        "name": "[('waiter', 'mcdonalds')]",
                        "percentage": "100.00%",
                        "duration": 100,
                        "children": [
                            {
                                "name": "[('server', 'kfc')]",
                                "percentage": "15.73%",
                                "duration": 100,
                                "children": [
                                    {
                                        "name": "[('server', 'wendys')]",
                                        "percentage": "12.64%",
                                        "duration": 100
                                    },
                                    {
                                        "name": "[('boss', 'dennys')]",
                                        "percentage": "10.96%",
                                        "duration": 100
                                    }
                                ]
                            },
                            {
                                "name": "[('cashier', 'chickfila')]",
                                "percentage": "10.40%",
                                "duration": 100,
                                "children": [
                                    {
                                        "name": "[('cashier', 'burger king')]",
                                        "percentage": "11.20%",
                                        "duration": 100
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

我想为每个孩子添加一个唯一的ID,该ID对应于他们所在的级别,因此最终看起来像这样,其中每个ID可以告诉您数据有多少父母,以及您对json的了解程度(例如,21.2.3.102将是第21个父母的第二个孩子的第3个孩子的第102个孩子):

{
"tree": {
    "name": "Top Level",
    "id": 1
    "children": [
        {
            "name": "[('server', 'Cheese')]",
            "id": 1.1
            "children": [
                {
                    "name": "[('waiter', 'mcdonalds')]",
                    "percentage": "100.00%",
                    "duration": 100,
                    "id": 1.1.1
                    "children": [
                        {
                            "name": "[('server', 'kfc')]",
                            "percentage": "15.73%",
                            "duration": 100,
                            "id": 1.1.1.1
                            "children": [
                                {
                                    "name": "[('server', 'wendys')]",
                                    "percentage": "12.64%",
                                    "duration": 100,
                                    "id":1.1.1.1.1
                                },
                                {
                                    "name": "[('boss', 'dennys')]",
                                    "percentage": "10.96%",
                                    "duration": 100,
                                    "id":1.1.1.1.2
                                }
                            ]
                        },
                        {
                            "name": "[('cashier', 'chickfila')]",
                            "percentage": "10.40%",
                            "duration": 100,
                            "id":1.1.1.2
                            "children": [
                                {
                                    "name": "[('cashier', 'burger king')]",
                                    "percentage": "11.20%",
                                    "duration": 100,
                                    "id":1.1.1.2.1
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
}

是否有一种简化的方法可以将其转换为包含许多子对象的超长json文件?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用递归遍历,其中d-来自json的字典:

def walk(d, level="1"):
    d["id"] = level
    for i, child in enumerate(d.get("children", []), 1):
        walk(child, level + "." + str(i))

walk(d["tree"])