简体   繁体   中英

How can I use python to add unique IDs to JSON children?

I have a json file that contains many children, like this:

{
    "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
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

I want to add a unique ID to each child that corresponds to the level they are in so it ends up looking like this, where each ID can tell how many parents the data has and how deep into the json you are (for example, 21.2.3.102 would be the 102nd child of a 3rd child of a 2nd child of the 21st parent):

{
"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
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
}

Is there a streamlined way to do this to a very long json file with many many children?

PLEASE

Thanks!

You can use recursion walking, where d - your dictionary from 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"])

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