使用python基于匹配键修剪json

2024-10-04 01:26:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON,如果符合某个条件,我将尝试在其中进行修剪。以下是我的JSON:

{'Items': [
    {'type': 'track', 'event': 'flag', 'properties': {'old': 'ABC11001'
        }, 'options': {'target': 'unflag'
        }, 'userId': None, 'anonymousId': 'c7ccc67e-f7d4-4198-9cef-7d6895c1bd3b', 'meta': {'timestamp': 1603043772959
        }, '_': {'originalAction': 'track', 'called': 'track', 'from': 'engineEnd'
        }, 'traits': {'lfid': 'foobar'
        }, 'id': '40cfb8a0-116b-11eb-9635-df40a485d353', 'id': 'footer', 'partner_resid': '934591a0-e05d-dd62-a55a-sh1735ec3981'
    },
    {'type': 'track', 'event': 'next', 'properties': {'old': 'ABC110023', 'new': 'ABC110026'
        }, 'options': {'target': 'nextTop'
        }, 'userId': None, 'anonymousId': 'c7ccc67e-f7d4-4198-9cef-7d6895c1bd3b', 'meta': {'timestamp': 1603043943118
        }, '_': {'originalAction': 'track', 'called': 'track', 'from': 'engineEnd'
        }, 'traits': {'lfid': 'foobar'
        }, 'id': 'a63ab410-116b-11eb-83a1-99c63d9410bc', 'lfid': 'foobar', 'partner_resid': '934591a0-e05d-dd62-a55a-sh1735ec3981'
    },
    {'type': 'track', 'event': 'flag', 'properties': {'old': 'ABC110099'
        }, 'options': {'target': 'unflag'
        }, 'userId': None, 'anonymousId': 'c7ccc67e-f7d4-4198-9cef-7d6895c1bd3b', 'meta': {'timestamp': 1603043137542
        }, '_': {'originalAction': 'track', 'called': 'track', 'from': 'engineEnd'
        }, 'traits': {'lfid': 'foobar'
        }, 'id': 'c6116880-1169-11eb-9880-c39a5007644a', 'lfid': 'foobar', 'partner_resid': '934591a0-e05d-dd62-a55a-sh1735ec3981'
    },
    {'type': 'track', 'event': 'flag', 'properties': {'new': 'ABC002234'
        }, 'options': {'target': 'flag'
        }, 'userId': None, 'anonymousId': 'c7ccc67e-f7d4-4198-9cef-7d6895c1bd3b', 'meta': {'timestamp': 1603042870105
        }, '_': {'originalAction': 'track', 'called': 'track', 'from': 'engineEnd'
        }, 'traits': {'lfid': 'foobar'
        }, 'id': '26a94d80-1169-11eb-9880-c39a5007644a', 'lfid': 'foobar', 'partner_resid': '934591a0-e05d-dd62-a55a-sh1735ec3981'
    },
    {'type': 'track', 'event': 'active', 'properties': {'new': 'ABC883322'
        }, 'options': {'target': 'bottomNext'
        }, 'userId': None, 'anonymousId': 'c7ccc67e-f7d4-4198-9cef-7d6895c1bd3b', 'meta': {'timestamp': 1603037276643
        }, '_': {'originalAction': 'track', 'called': 'track', 'from': 'engineEnd'
        }, 'traits': {'lfid': 'foobar'
        }, 'id': '20b1aab0-115c-11eb-9e18-b5713e730a08', 'lfid': 'foobar', 'partner_resid': '958791a0-e05d-4e01-a55a-da48e3ec3981'
    },
  }

在这里,我尝试使用event具有activeproperties仅具有new但未能产生: 下面是我的代码:

    for idx, elem in enumerate(json_result['Items']):
        
        if json_result.get("Items").get("elem").properties.get("old") in elem.keys():
            del json_result['Items'][idx]

不知道我哪里做错了

result_json应仅包含:

        {'type': 'track', 'event': 'active', 'properties': {'new': 'ABC883322'
            }, 'options': {'target': 'bottomNext'
            }, 'userId': None, 'anonymousId': 'c7ccc67e-f7d4-4198-9cef-7d6895c1bd3b', 'meta': {'timestamp': 1603037276643
            }, '_': {'originalAction': 'track', 'called': 'track', 'from': 'engineEnd'
            }, 'traits': {'lfid': 'foobar'
            }, 'id': '20b1aab0-115c-11eb-9e18-b5713e730a08', 'lfid': 'foobar', 'partner_resid': '958791a0-e05d-4e01-a55a-da48e3ec3981'}

Tags: noneeventidtargettypetrackpropertiesmeta
1条回答
网友
1楼 · 发布于 2024-10-04 01:26:32

您的json(缺少结束列表括号)中确实有错误>;如果我理解正确,您只想从json列表中筛选那些具有event=active和键"new" in properties的对象。为什么不创建新的结果列表并只存储匹配的对象呢

items = dct["Items"]
result = []
for obj in items:
    if obj["event"] == "active" and "new" in obj["properties"]:
        result.append(obj)

相关问题 更多 >