0

I am trying to create lists from json datas by pulling one by one and append them to the lists. However, some variables does not given in all json files. For example: for the json file below, data does not have ['statistics']['aerialLost'] , so it return Key Error. My Expected solution is when json file does not have key, append 'None' value to the list and continue.

Code

s_aerialLost = []
s_aerialWon = []
s_duelLost = []
s_duelWon = []

players = ['Martin Linnes', 'Christian Luyindama', 'Marcão', 'Ömer Bayram', 'Oghenekaro Etebo', 'Muhammed Kerem Aktürkoğlu', 'Gedson Fernandes', 'Emre Kılınç', 'Ryan Babel', 'Mostafa Mohamed', 'Florent Hadergjonaj', 'Tomáš Břečka', 'Duško Tošić', 'Oussama Haddadi', 'Kristijan Bistrović', 'Aytaç Kara', 'Haris Hajradinović', 'Armin Hodžić', 'Gilbert Koomson', 'Isaac Kiese Thelin']
players_id = [109569, 867191, 840951, 68335, 839110, 903324, 862055, 202032, 1876, 873551, 354860, 152971, 14557, 867180, 796658, 128196, 254979, 138127, 341107, 178743]

for player, player_id in zip(players, players_id):
    
    url = base_url + str(player_id)
    data = requests.request("GET", url).json()

## just added 4 data for simplify
 
    accurateLongBalls = str(data['statistics']['accurateLongBalls'])
    aerialLost = str(data['statistics']['aerialLost'])
    aerialWon = str(data['statistics']['aerialWon'])
    duelLost = str(data['statistics']['duelLost'])
    s_aerialLost.append()
    s_aerialWon.append()
    s_duelLost.append()
    s_duelWon.append()

Json File

{
  "player": {
    "name": "Martin Linnes",
    "slug": "martin-linnes",
    "shortName": "M. Linnes",
    "position": "D",
    "userCount": 339,
    "id": 109569,
    "marketValueCurrency": "€",
    "dateOfBirthTimestamp": 685324800
  },
  "team": {
    "name": "Galatasaray",
    "slug": "galatasaray",
    "shortName": "Galatasaray",
    "gender": "M",
    "userCount": 100254,
    "nameCode": "GAL",
    "national": false,
    "type": 0,
    "id": 3061,
    "teamColors": {
      "primary": "#ff9900",
      "secondary": "#ff0000",
      "text": "#ff0000"
    }
  },
  "statistics": {
    "totalPass": 32,
    "accuratePass": 22,
    "totalLongBalls": 7,
    "accurateLongBalls": 3,
    "totalCross": 2,
    "aerialWon": 1,
    "duelLost": 2,
    "duelWon": 7,
    "totalContest": 3,
    "wonContest": 2,
    "totalClearance": 4,
    "totalTackle": 3,
    "wasFouled": 1,
    "fouls": 1,
    "minutesPlayed": 82,
    "touches": 63,
    "rating": 7.3,
    "possessionLostCtrl": 18,
    "keyPass": 1
  },
  "position": "D"
}

Error

KeyError: 'aerialLost'
4
  • 1
    Use dictionary.get('key') instead of dictionary['key']. It returns None by default instead of raising an error. Commented Feb 18, 2021 at 20:08
  • i changed variables like player_name = str(data['player']['name']) to player_name = str(data.get('player')('name')) . It gives TypeError: 'dict' object is not callable error. Commented Feb 18, 2021 at 20:15
  • data.get('player', {}).get('name') Commented Feb 18, 2021 at 20:16
  • You have to use .get() at each level of nesting. Commented Feb 18, 2021 at 20:17

1 Answer 1

1

Use .get(). You can specify a default value to return if the key is not found, and it defaults to None.

So you can use

aerialLost = str(data.get('statistics', {}).get('aerialLost'))

The first call defaults to an empty dictionary so that there's something to make the second .get() call on. The second call just returns the default None.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.