在python中搜索json文件中的值组合

hsgswve4  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(118)

我有以下json:

{
        "comment": "created 2023-08-03 00:07",
        "createdBy": "yours truely",
        "endsAt": "2023-08-03T02:07:43.141Z",
        "id": "907ce71d-e0f3-4ee4-83af-042346c6d89f",
        "matchers": [
            {
                "isEqual": true,
                "isRegex": false,
 
                "name": "alertname",
                "value": "myAlertName_1"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "application",
                "value": "myApplication"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "rule_uid",
                "value": "bf1a1b9d-1962-4b90-a9af-972baa1cc034"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "severity",
                "value": "3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "team",
                "value": "teamA"
            }
        ],
        "startsAt": "2023-08-02T22:08:06.000Z",
        "status": {
            "state": "expired"
        },
        "updatedAt": "2023-08-02T22:08:06.000Z"
    },
{
        "comment": "created 2023-08-03 00:07",
        "createdBy": "yours truely",
        "endsAt": "2023-08-03T02:07:43.141Z",
        "id": "907ce71d-e0f3-4ee4-83af-042346c6d89f",
        "matchers": [
            {
                "isEqual": true,
                "isRegex": false,
                "name": "alertname",
                "value": "myAlertName_2"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "application",
                "value": "myApplication"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "rule_uid",
                "value": "bf1a1b9d-1962-4b90-a9af-972baa1cc034"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "severity",
                "value": "3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "team",
                "value": "teamB"
            }
        ],
        "startsAt": "2023-08-02T22:08:06.000Z",
        "status": {
            "state": "expired"
        },
        "updatedAt": "2023-08-02T22:08:06.000Z"
    },
{
        "comment": "created 2023-08-03 00:07",
        "createdBy": "yours truely",
        "endsAt": "2023-08-03T02:07:43.141Z",
        "id": "907ce71d-e0f3-4ee4-83af-042346c6d89f",
        "matchers": [
            {
                "isEqual": true,
                "isRegex": false,
                "name": "alertname",
                "value": "myAlertName_3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "application",
                "value": "myApplication"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "rule_uid",
                "value": "bf1a1b9d-1962-4b90-a9af-972baa1cc034"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "severity",
                "value": "3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "team",
                "value": "teamA"
            }
        ],
        "startsAt": "2023-08-02T22:08:06.000Z",
        "status": {
            "state": "expired"
        },
        "updatedAt": "2023-08-02T22:08:06.000Z"
    },

字符串
我想在设置以下内容的地方添加额外的警报(包含完整的详细信息)

  • 第一个月

所以我期待从myAlertName_1myAlertName_3的完整输出,我在这条路线上,但任何路线都很好。

with open(datfile) as jsondata:
        jdata = json.load(jsondata)
    print(list(filter(lambda x:x[sname]==svalue,jdata)))


请建议我归档此JSON,以便只获取其中application == myApplication和其中team == teamA的条目

pkbketx9

pkbketx91#

首先,你的json输入可能是一个字典列表:在您的示例中缺少外部方括号([...])。
然后你可以简单地检查列表中的所有元素,并检查matchers中元素的名称/值:

import json

jdata = json.loads(data_json)
data_out = []
for alert in jdata:
    app_match = False
    team_match = False
    for m in alert['matchers']:
        if m['name'] == 'application' and m['value'] == 'myApplication':
            app_match = True
        if m['name'] == 'team' and m['value'] == 'teamA':
            team_match = True
    if app_match and team_match:
        data_out.append(alert)

字符串
然后,data_out将包含myAlertName_1myAlertName_3的字典
在我的示例中,data_json等于以下字符串(它将是代码片段中datfile的内容):

[{
        "comment": "created 2023-08-03 00:07",
        "createdBy": "yours truely",
        "endsAt": "2023-08-03T02:07:43.141Z",
        "id": "907ce71d-e0f3-4ee4-83af-042346c6d89f",
        "matchers": [
            {
                "isEqual": true,
                "isRegex": false,
 
                "name": "alertname",
                "value": "myAlertName_1"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "application",
                "value": "myApplication"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "rule_uid",
                "value": "bf1a1b9d-1962-4b90-a9af-972baa1cc034"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "severity",
                "value": "3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "team",
                "value": "teamA"
            }
        ],
        "startsAt": "2023-08-02T22:08:06.000Z",
        "status": {
            "state": "expired"
        },
        "updatedAt": "2023-08-02T22:08:06.000Z"
    },
{
        "comment": "created 2023-08-03 00:07",
        "createdBy": "yours truely",
        "endsAt": "2023-08-03T02:07:43.141Z",
        "id": "907ce71d-e0f3-4ee4-83af-042346c6d89f",
        "matchers": [
            {
                "isEqual": true,
                "isRegex": false,
                "name": "alertname",
                "value": "myAlertName_2"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "application",
                "value": "myApplication"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "rule_uid",
                "value": "bf1a1b9d-1962-4b90-a9af-972baa1cc034"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "severity",
                "value": "3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "team",
                "value": "teamB"
            }
        ],
        "startsAt": "2023-08-02T22:08:06.000Z",
        "status": {
            "state": "expired"
        },
        "updatedAt": "2023-08-02T22:08:06.000Z"
    },
{
        "comment": "created 2023-08-03 00:07",
        "createdBy": "yours truely",
        "endsAt": "2023-08-03T02:07:43.141Z",
        "id": "907ce71d-e0f3-4ee4-83af-042346c6d89f",
        "matchers": [
            {
                "isEqual": true,
                "isRegex": false,
                "name": "alertname",
                "value": "myAlertName_3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "application",
                "value": "myApplication"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "rule_uid",
                "value": "bf1a1b9d-1962-4b90-a9af-972baa1cc034"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "severity",
                "value": "3"
            },
            {
                "isEqual": true,
                "isRegex": false,
                "name": "team",
                "value": "teamA"
            }
        ],
        "startsAt": "2023-08-02T22:08:06.000Z",
        "status": {
            "state": "expired"
        },
        "updatedAt": "2023-08-02T22:08:06.000Z"
    }
]

相关问题