展平动态嵌套JSON

时间:2020-05-14 15:16:35

标签: python json python-3.x

我正在尝试使用如下所示的python扁平化多层次JSON。在第1级之后扁平化时我遇到了问题。

[
    {
        "ID": "PRED",
        "fields": [
            "id",,
            "name",
            "parent"
        ],
        "items": [
            {
                "ref": "#55:130",
                "id": "122",
                "values": [
                    "1904",
                    "DD",
                    "PP-PADK"
                ],
                "relationships": [
                    {
                        "screen1": "menu",
                        "relationships": [
                            {
                                "id": "157"
                            },
                            {
                                "id": "158"
                            }
                        ]
                    },
                    {
                        "screen2": "home",
                        "relationships": [
                            {
                                "id": "4",
                                "relTypeId": 2,
                                "relTypeName": "Is required"
                            },
                            {
                                "id": "7",
                                "relTypeId": 2,
                                "relTypeName": "Is required"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

我尝试过以下方法:

for item in r["items"]:
    for value in item["values"]:
        result += value + ","
    result = result.rstrip(",") + "\n"
result = result.strip()

所需的o / p列为: id,名称,父项,menu_id1,menu_id2,home_id1,relTypeId1,relTypeName1,home_id2,relTypeId2,relTypeName2
1904,DD,PP-PADK,157,158,4,2,是必需的,7,2,是必需的

分别在screen1和screen2中的关系中可以有任意多个元素。上师的帮助。

0 个答案:

没有答案