0

I have got the following 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"
    },

I want to extra the alerts(with full details) where the following is set

  • application = myApplication && team = teamA

So i am expecting full output from myAlertName_1 and myAlertName_3 I am on this route but any route is fine..

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

Please advice me on to file this json to get only entries where application == myApplication, and where team == teamA

1 Answer 1

0

First your json input is probably meant to be a list of dictionary: the outer square brackets ([...]) are missing in your example.

Then you can simply go over all elements of the list and check for the names/values of the elements in 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 will then contain the dictionaries for myAlertName_1 and myAlertName_3

data_json is equal to the following string in my example (it would be the content of datfile in your snippet):

[{
        "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"
    }
]
2
  • Thank you! I will try this, this looks good, and i think it is what i am looking for. I ll supply an update shortly
    – Arch
    Commented Aug 3, 2023 at 11:10
  • in your solution the last if will fail if one of the selectors is not there. It will not show the list. If i indent the last if it works correct. Thank you
    – Arch
    Commented Aug 3, 2023 at 14:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.