递归遍历 Python 中的参差不齐的 JSON 层次结构 3 以执行部分叶节点删除
Recursively Traversing Ragged JSON Hierarchy in Python 3 to perform a partial leaf node deletion
所以,我有一个 JSON 块,看起来有点像这样:
[
{
"children": [
{
"address": "123 Main Street",
"class": "blarg",
"children": [
{
"children": [
{
"children": [
{
"y_type": "string",
"x_type": "002",
"002_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field020": "AAA",
"field030": "DDD"
},
{
"y_type": "bool",
"x_type": "007",
"007_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field1": True,
"field2": True
}
],
"create_at": "2018-03-29T13:45:20.875Z",
"x_id": "3e0e1b44-ac0d-4bf7-985e-11d74b8be323",
"junk_field": {},
"x_type": "000",
"timezone": "America/New_York",
"update_at": "2018-03-29T13:45:20.875Z"
},
{
"sibling": [1,2,3]
}
]
}
]
}
]
}]
我需要做的是找到 "children" 个属于 x_type“007”的叶节点,并删除与该数据块关联的字段 1 条目。我在尝试隔离仅与正确类型的叶节点(子节点,而不是兄弟节点)关联的整个字典时遇到问题,以便我可以检查它是否正确 x_type 并执行删除操作。
我不确定要从我拼凑的递归函数中传递什么样的值 to/back。我以前从未在 Python 中做过递归,更不用说针对参差不齐的层次结构 JSON,所以我可以使用一些 help/guidance 来了解 use/google 的方法。如果你能给我任何帮助,让我朝着正确的方向前进,我将不胜感激!!
您可以通过递归使用字典解包:
def d_filter(d):
return {**({a:b for a, b in d.items() if d.get('x_type') != '007' or a != 'field1'}), \
'children':list(map(d_filter, d.get('children', [])))}
new_data = list(map(d_filter, data))
import json
print(json.dumps(new_data, indent=4))
输出:
[
{
"children": [
{
"address": "123 Main Street",
"class": "blarg",
"children": [
{
"children": [
{
"children": [
{
"y_type": "string",
"x_type": "002",
"002_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field020": "AAA",
"field030": "DDD",
"children": []
},
{
"y_type": "bool",
"x_type": "007",
"007_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field2": true,
"children": []
}
],
"create_at": "2018-03-29T13:45:20.875Z",
"x_id": "3e0e1b44-ac0d-4bf7-985e-11d74b8be323",
"junk_field": {},
"x_type": "000",
"timezone": "America/New_York",
"update_at": "2018-03-29T13:45:20.875Z"
},
{
"sibling": [
1,
2,
3
],
"children": []
}
]
}
]
}
]
}
]
所以,我有一个 JSON 块,看起来有点像这样:
[
{
"children": [
{
"address": "123 Main Street",
"class": "blarg",
"children": [
{
"children": [
{
"children": [
{
"y_type": "string",
"x_type": "002",
"002_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field020": "AAA",
"field030": "DDD"
},
{
"y_type": "bool",
"x_type": "007",
"007_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field1": True,
"field2": True
}
],
"create_at": "2018-03-29T13:45:20.875Z",
"x_id": "3e0e1b44-ac0d-4bf7-985e-11d74b8be323",
"junk_field": {},
"x_type": "000",
"timezone": "America/New_York",
"update_at": "2018-03-29T13:45:20.875Z"
},
{
"sibling": [1,2,3]
}
]
}
]
}
]
}]
我需要做的是找到 "children" 个属于 x_type“007”的叶节点,并删除与该数据块关联的字段 1 条目。我在尝试隔离仅与正确类型的叶节点(子节点,而不是兄弟节点)关联的整个字典时遇到问题,以便我可以检查它是否正确 x_type 并执行删除操作。
我不确定要从我拼凑的递归函数中传递什么样的值 to/back。我以前从未在 Python 中做过递归,更不用说针对参差不齐的层次结构 JSON,所以我可以使用一些 help/guidance 来了解 use/google 的方法。如果你能给我任何帮助,让我朝着正确的方向前进,我将不胜感激!!
您可以通过递归使用字典解包:
def d_filter(d):
return {**({a:b for a, b in d.items() if d.get('x_type') != '007' or a != 'field1'}), \
'children':list(map(d_filter, d.get('children', [])))}
new_data = list(map(d_filter, data))
import json
print(json.dumps(new_data, indent=4))
输出:
[
{
"children": [
{
"address": "123 Main Street",
"class": "blarg",
"children": [
{
"children": [
{
"children": [
{
"y_type": "string",
"x_type": "002",
"002_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field020": "AAA",
"field030": "DDD",
"children": []
},
{
"y_type": "bool",
"x_type": "007",
"007_id": 2222,
"updated_at": "2018-03-29T13:47:42.986Z",
"field2": true,
"children": []
}
],
"create_at": "2018-03-29T13:45:20.875Z",
"x_id": "3e0e1b44-ac0d-4bf7-985e-11d74b8be323",
"junk_field": {},
"x_type": "000",
"timezone": "America/New_York",
"update_at": "2018-03-29T13:45:20.875Z"
},
{
"sibling": [
1,
2,
3
],
"children": []
}
]
}
]
}
]
}
]