Home > Enterprise >  How to creating recursive JSON hierarchy tree?
How to creating recursive JSON hierarchy tree?

Time:09-15

You can see the name of parent is color start with child_id 0 and children is red and blue that binds id from color.

I like to get the data in the table to make it into Json how to do?

enter image description here

My data

import pandas as pd

data = {
    'id': ['2', '13', '14', '15'],
    'name': ['color', 'red', 'blue', 'ruby red'],
    'child_id': ['0', '2', '2', '13']
}
  

df = pd.DataFrame(data)
df

Expected Output

[
    {
        "name": "color",
        "id": 2,
        "child_id": 0,
        "children": [
            {
                "name": "red",
                "id": 13,
                "child_id": 2,
                "children": [
                    {
                        "name": "ruby red",
                        "id": 15,
                        "child_id": 13,
                    }
                ]
            },
            {
                "name": "blue",
                "id": 14,
                "child_id": 2
            }
        ]
    }
]

CodePudding user response:

Consider it as a tree and apply simple traversal on the dictionary, which is converted from df by df.to_dict(). Type annotations are added. Hope you can understand.

from dataclasses import dataclass, field, asdict
from pprint import pprint
from typing import List, Dict, Optional
import pandas as pd
from pandas import DataFrame


@dataclass
class Tree:
    name: str
    id: int
    child_id: int
    children: List['Tree'] = field(default_factory=list)


def traversal(df: DataFrame) -> Optional[Tree]:
    tmp: List[Tree] = [Tree(name=i["name"], id=int(i["id"]), child_id=int(i["child_id"])) for i in df.to_dict("records")]
    memo: Dict[int, Tree] = {i.id: i for i in tmp}
    root = None
    for i in tmp:
        if i.child_id == 0:
            root = i
        else:
            memo[i.child_id].children.append(i)
    return root


if __name__ == '__main__':
    data = {
        'id': ['2', '13', '14', '15'],
        'name': ['color', 'red', 'blue', 'ruby red'],
        'child_id': ['0', '2', '2', '13']
    }
    dataframe = pd.DataFrame(data)
    res = traversal(dataframe)
    if res:
        pprint([asdict(res)])
    else:
        print("err")

Output:

[{'child_id': 0,
  'children': [{'child_id': 2,
                'children': [{'child_id': 13,
                              'children': [],
                              'id': 15,
                              'name': 'ruby red'}],
                'id': 13,
                'name': 'red'},
               {'child_id': 2, 'children': [], 'id': 14, 'name': 'blue'}],
  'id': 2,
  'name': 'color'}]
  • Related