Skip to content
Advertisement

Iterate and Parse Json with similar values in python

I have this code which iterates and parses data from a json API feed. I am trying to filter out just the Premier League games but some competitions have similar names like ‘Premier League 2 (Division 2)’ and ‘Premier League Cup’

    for event_data in data['events']:
        if event_data['path'][1]['name'] == 'England' and 'Premier League' in event_data['path'][2]['name'] and 'MATCH' in event_data['tags']:
            competition = event_data['group']
            competition_id = event_data['groupId']
            event_id = event_data['id']
            event_name = event_data['name']
            home_team = event_data['homeName']
            away_team = event_data['awayName']
            event_start = event_data['start']
            event_status = event_data['state']

            print(f'{competition} {competition_id} {event_id} {event_name} {event_start} {event_status} {home_team} {away_team}')

the output for this code is below. As you can see the first 2 lines are similar competition names to ‘Premier League’. I tried to filter by the competition ID (second column) but integers seem to cause an issue using this method.

Premier League 2 (Division 2) 2000096352 1019103797 Nottingham Forest U21 - Stoke City Reserves 2022-10-17T18:00:00Z NOT_STARTED Nottingham Forest U21 Stoke City Reserves
Premier League Cup 2000099592 1019095920 Middlesbrough U21 - West Bromwich Reserves 2022-10-17T17:00:00Z NOT_STARTED Middlesbrough U21 West Bromwich Reserves
Premier League 1000094985 1018752216 Brighton & Hove Albion - Nottingham Forest 2022-10-18T18:30:00Z NOT_STARTED Brighton & Hove Albion Nottingham Forest
Premier League 1000094985 1018752209 Crystal Palace - Wolves 2022-10-18T19:15:00Z NOT_STARTED Crystal Palace Wolves
Premier League 1000094985 1018752210 Bournemouth - Southampton 2022-10-19T18:30:00Z NOT_STARTED Bournemouth Southampton
Premier League 1000094985 1018752206 Brentford - Chelsea 2022-10-19T18:30:00Z NOT_STARTED Brentford Chelsea
Premier League 1000094985 1018752217 Liverpool - West Ham 2022-10-19T18:30:00Z NOT_STARTED Liverpool West Ham
Premier League 1000094985 1018752207 Newcastle United - Everton 2022-10-19T18:30:00Z NOT_STARTED Newcastle United Everton
Premier League 1000094985 1018752214 Manchester United - Tottenham 2022-10-19T19:15:00Z NOT_STARTED Manchester United Tottenham
Premier League 1000094985 1018752213 Fulham - Aston Villa 2022-10-20T18:30:00Z NOT_STARTED Fulham Aston Villa
Premier League 1000094985 1018752211 Leicester City - Leeds United 2022-10-20T19:15:00Z NOT_STARTED Leicester City Leeds United
Premier League 1000094985 1018752197 Nottingham Forest - Liverpool 2022-10-22T11:30:00Z NOT_STARTED Nottingham Forest Liverpool
Premier League 1000094985 1018752189 Everton - Crystal Palace 2022-10-22T14:00:00Z NOT_STARTED Everton Crystal Palace
Premier League 1000094985 1018752200 Manchester City - Brighton & Hove Albion 2022-10-22T14:00:00Z NOT_STARTED Manchester City Brighton & Hove Albion
Premier League 1000094985 1018752192 Chelsea - Manchester United 2022-10-22T16:30:00Z NOT_STARTED Chelsea Manchester United
Premier League 1000094985 1018752195 Aston Villa - Brentford 2022-10-23T13:00:00Z NOT_STARTED Aston Villa Brentford
Premier League 1000094985 1018752203 Leeds United - Fulham 2022-10-23T13:00:00Z NOT_STARTED Leeds United Fulham
Premier League 1000094985 1018752193 Southampton - Arsenal 2022-10-23T13:00:00Z NOT_STARTED Southampton Arsenal
Premier League 1000094985 1018752201 Wolves - Leicester City 2022-10-23T13:00:00Z NOT_STARTED Wolves Leicester City
Premier League 1000094985 1018752190 Tottenham - Newcastle United 2022-10-23T15:30:00Z NOT_STARTED Tottenham Newcastle United
Premier League 1000094985 1018752204 West Ham - Bournemouth 2022-10-24T19:00:00Z NOT_STARTED West Ham Bournemouth

what would be the best way to filter these two competitions out of the output?

Premier League 2 (Division 2) 2000096352 1019103797 Nottingham Forest U21 - Stoke City Reserves 2022-10-17T18:00:00Z NOT_STARTED Nottingham Forest U21 Stoke City Reserves
Premier League Cup 2000099592 1019095920 Middlesbrough U21 - West Bromwich Reserves 2022-10-17T17:00:00Z NOT_STARTED Middlesbrough U21 West Bromwich Reserves

This is what the json looks like for the Premier League games:

{
    "englishName": "Brighton & Hove Albion - Nottingham Forest",
    "groupId": 1000094985,
    "homeName": "Brighton & Hove Albion",
    "groupSortOrder": 3001001000000000000,
    "path": 
    [
        {
            "englishName": "Football",
            "termKey": "football",
            "name": "Soccer",
            "id": 1000093190
        },
        {
            "englishName": "England",
            "termKey": "england",
            "name": "England",
            "id": 1000461733
        },
        {
            "englishName": "Premier League",
            "termKey": "premier_league",
            "name": "Premier League",
            "id": 1000094985
        }
    ],
    "id": 1018752216,
    "state": "NOT_STARTED",
    "group": "Premier League",
    "start": "2022-10-18T18:30:00Z",
    "nonLiveBoCount": 243,
    "tags": 
    [
        "OFFERED_LIVE",
        "BET_BUILDER",
        "MATCH"
    ],
    "awayName": "Nottingham Forest",
    "name": "Brighton & Hove Albion - Nottingham Forest",
    "sport": "FOOTBALL"
}

this is the JSON for ‘Premier League 2 (Division 2)

{
    "englishName": "Nottingham Forest U21 - Stoke City Reserves",
    "groupId": 2000096352,
    "homeName": "Nottingham Forest U21",
    "groupSortOrder": 3001999000000000000,
    "path": 
    [
        {
            "englishName": "Football",
            "termKey": "football",
            "name": "Soccer",
            "id": 1000093190
        },
        {
            "englishName": "England",
            "termKey": "england",
            "name": "England",
            "id": 1000461733
        },
        {
            "englishName": "Premier League 2 (Division 2)",
            "termKey": "premier_league_2__division_2_",
            "name": "Premier League 2 (Division 2)",
            "id": 2000096352
        }
    ],
    "id": 1019103797,
    "state": "NOT_STARTED",
    "group": "Premier League 2 (Division 2)",
    "start": "2022-10-17T18:00:00Z",
    "nonLiveBoCount": 14,
    "tags": 
    [
        "OFFERED_LIVE",
        "BET_BUILDER",
        "MATCH"
    ],
    "awayName": "Stoke City Reserves",
    "name": "Nottingham Forest U21 - Stoke City Reserves",
    "sport": "FOOTBALL"
}

and this is the JSON for “Premier League Cup’

{
    "englishName": "Middlesbrough U21 - West Bromwich Reserves",
    "groupId": 2000099592,
    "homeName": "Middlesbrough U21",
    "groupSortOrder": 3001999000000000000,
    "path": 
    [
        {
            "englishName": "Football",
            "termKey": "football",
            "name": "Soccer",
            "id": 1000093190
        },
        {
            "englishName": "England",
            "termKey": "england",
            "name": "England",
            "id": 1000461733
        },
        {
            "englishName": "Premier League Cup",
            "termKey": "premier_league_cup",
            "name": "Premier League Cup",
            "id": 2000099592
        }
    ],
    "id": 1019095920,
    "state": "NOT_STARTED",
    "group": "Premier League Cup",
    "start": "2022-10-17T17:00:00Z",
    "nonLiveBoCount": 14,
    "tags": 
    [
        "OFFERED_LIVE",
        "BET_BUILDER",
        "MATCH"
    ],
    "awayName": "West Bromwich Reserves",
    "name": "Middlesbrough U21 - West Bromwich Reserves",
    "sport": "FOOTBALL"
}

Advertisement

Answer

Looks like you can update the conditional to filter on the groupId or group name

if event_data['path'][1]['name'] == 'England' and event_data['groupId'] == 1000094985 and 'MATCH' in event_data['tags']:

or

if event_data['path'][1]['name'] == 'England' and event_data['group'] == 'Premier League' and 'MATCH' in event_data['tags']:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement