Home > Mobile >  How to only interact with objects that are after specific object value in Json using Python?
How to only interact with objects that are after specific object value in Json using Python?

Time:03-18

I'm collecting data that is added to a JSON sequentially, but since they are all added together, they don't come well separated, so I need to get around this difficulty.

My JSON follows this pattern:

{
    "content": {
      "matchFacts": {
        "events": {
          "events": [
            {
              "type": "Substitution",
              "time": 33
            },
            {
              "type": "Card",
              "time": 34
            },
            {
              "type": "Goal",
              "time": 38            
            },
            {
              "type": "Card",
              "time": 43            
            },
            {
              "type": "AddedTime",
              "time": 45
            },
            {
              "type": "Goal",
              "time": 45
            },
            {
              "type": "Half",
              "time": 45
            },
            {
              "type": "Substitution",
              "time": 46
            },
            {
              "type": "Substitution",
              "time": 58
            },
            {
              "type": "Substitution",
              "time": 58
            },
            {
              "type": "Goal",
              "time": 71
            },
            {
              "type": "Substitution",
              "time": 74
            },
            {
              "type": "Substitution",
              "time": 74
            },
            {
              "type": "Substitution",
              "time": 77
            },
            {
              "type": "Substitution",
              "time": 77
            },
            {
              "type": "Substitution",
              "time": 77
            },
            {
              "type": "Substitution",
              "time": 83
            },
            {
              "type": "AddedTime",
              "time": 90
            },
            {
              "type": "Card",
              "time": 90
            },
            {
              "type": "Half",
              "time": 90
            }
          ]
      }
    }
  }
}

Let's say I need to collect only the "type": "Goal" that are before the first "type": "Half" appears, so I do it like this:

search_goals = response_json['content']['matchFacts']['events']['events']
goals_ht = 0
for event in search_goals:
    if (event['type'] == 'Half'):
        break
    elif (event['type'] == 'Goal'):
        goals_ht  = 1

But what about when I need to collect only "type": "Goal" that are AFTER the first "type": "Half", how should I proceed?

CodePudding user response:

Given your structure, a possible solution is to create a False flag that flips to True once a Half event is reached, then simply check for that flag when doing something to the Goal events:

search_goals = response_json['content']['matchFacts']['events']['events']

reached_ht = False
for event in search_goals:
    if event['type'] == 'Half':
        reached_ht = True
    elif event['type'] == 'Goal' and reached_ht:
        # ...
  • Related