python 阅读json对象并删除一些值

lx0bsm1f  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(226)

我有一个JSON对象,结构如下:

{
    "org_name": "1",
    "available": True,
    "orgs": [
        {
            "org_name": "2",
            "available": False,
            "orgs": []
        },
        {
            "org_name": "3",
            "available": False,
            "orgs": []
        },
        {
            "org_name": "4",
            "available": True,
            "orgs": []
        },
        {
            "org_name": "5",
            "available": False,
            "orgs": [
                {
                    "org_name": "6",
                    "available": False,
                    "orgs": []
                },
                {
                    "org_name": "7",
                    "available": False,
                    "orgs": []
                },
                {
                    "org_name": "8",
                    "available": False,
                    "orgs": [
                        {
                            "org_name": "9",
                            "available": False,
                            "orgs": []
                        },
                        {
                            "org_name": "10",
                            "available": True,
                            "orgs": []
                        }
                    ]
                }
            ]
        }
    ]
}

我想解析这个JSON数据并得到类似于下面的输出:

{
  "org_name": "1",
  "orgs": [
    {
      "org_name": "4",
      "orgs": []
    },
    {
      "org_name": "5",
      "orgs": [
        {
          "org_name":"8",
           "orgs":[
           {
           "org_name":"10"
           }
         ]
        }
      ]
    }
}

这些条件是:

  • 如果availableTrue,则输出应包含它的org_name和它的orgs;
  • 如果availableFalse,则应将其删除;
  • 如果子组织org具有它的availableTrue,则应该存在完整的父组织。

我很难找到一个逻辑来获得输出。
如有任何帮助,

6fe3ivhb

6fe3ivhb1#

您需要编写一个简单的递归函数,该函数将检查JSON对象是否可用或其任何子节点是否可用:

def filter_orgs(source):
    orgs = [org for o in source["orgs"] if (org := filter_orgs(o))]
    if orgs or source["available"]:
        return {
            "org_name": source["org_name"], 
            "orgs": orgs
        }

用法:

source = {
    "org_name": "1",
    "available": True,
    "orgs": [
        {
            "org_name": "2",
            "available": False,
            "orgs": []
        },
        {
            "org_name": "3",
            "available": False,
            "orgs": []
        },
        {
            "org_name": "4",
            "available": True,
            "orgs": []
        },
        {
            "org_name": "5",
            "available": False,
            "orgs": [
                {
                    "org_name": "6",
                    "available": False,
                    "orgs": []
                },
                {
                    "org_name": "7",
                    "available": False,
                    "orgs": []
                },
                {
                    "org_name": "8",
                    "available": False,
                    "orgs": [
                        {
                            "org_name": "9",
                            "available": False,
                            "orgs": []
                        },
                        {
                            "org_name": "10",
                            "available": True,
                            "orgs": []
                        }
                    ]
                }
            ]
        }
    ]
}

filtered_source = filter_orgs(source)

输出:

{
    "org_name": "1",
    "orgs": [
        {
            "org_name": "4",
            "orgs": []
        },
        {
            "org_name": "5",
            "orgs": [
                {
                    "org_name": "8",
                    "orgs": [
                        {
                            "org_name": "10",
                            "orgs": []
                        }
                    ]
                }
            ]
        }
    ]
}
    • 更新**也可以就地修改字典:
def filter_orgs_inplace(source):
    available = source.pop("available")
    for i in range(len(orgs := source["orgs"]) - 1, -1, -1):
        if not filter_orgs_inplace(orgs[i]):
            del orgs[i]
        else:
            available = True
    if not available:
        source.clear()
    return available

用法:

source = {
    "org_name": "1",
    "available": True,
    "orgs": [
        {
            "org_name": "2",
            "available": False,
            "orgs": []
        },
        {
            "org_name": "3",
            "available": False,
            "orgs": []
        },
        {
            "org_name": "4",
            "available": True,
            "orgs": []
        },
        {
            "org_name": "5",
            "available": False,
            "orgs": [
                {
                    "org_name": "6",
                    "available": False,
                    "orgs": []
                },
                {
                    "org_name": "7",
                    "available": False,
                    "orgs": []
                },
                {
                    "org_name": "8",
                    "available": False,
                    "orgs": [
                        {
                            "org_name": "9",
                            "available": False,
                            "orgs": []
                        },
                        {
                            "org_name": "10",
                            "available": True,
                            "orgs": []
                        }
                    ]
                }
            ]
        }
    ]
}

filter_orgs_inplace(source)  # modifies source

输出:

{
    "org_name": "1",
    "orgs": [
        {
            "org_name": "4",
            "orgs": []
        },
        {
            "org_name": "5",
            "orgs": [
                {
                    "org_name": "8",
                    "orgs": [
                        {
                            "org_name": "10",
                            "orgs": []
                        }
                    ]
                }
            ]
        }
    ]
}

相关问题