我有以下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_1
和myAlertName_3
的完整输出,我在这条路线上,但任何路线都很好。
with open(datfile) as jsondata:
jdata = json.load(jsondata)
print(list(filter(lambda x:x[sname]==svalue,jdata)))
型
请建议我归档此JSON,以便只获取其中application == myApplication
和其中team == teamA
的条目
1条答案
按热度按时间pkbketx91#
首先,你的json输入可能是一个字典列表:在您的示例中缺少外部方括号(
[...]
)。然后你可以简单地检查列表中的所有元素,并检查
matchers
中元素的名称/值:字符串
然后,
data_out
将包含myAlertName_1
和myAlertName_3
的字典在我的示例中,
data_json
等于以下字符串(它将是代码片段中datfile
的内容):型