1

I am trying to parse two items from a JSON file list, but getting an error TypeError: list indices must be integers or slices, not str

My current script is:

with open("match.json",'r') as f:
    data = json.load(f)
    f.close()

print(type(data['h']))
data['h']

data['h']['X']

And below is the JSON i am working with:

{'h': [{'id': '310295',
   'minute': '6',
   'result': 'SavedShot',
   'X': '0.8280000305175781',
   'Y': '0.639000015258789',
   'xG': '0.04247729107737541',
   'player': 'Anthony Martial',
   'h_a': 'h',
   'player_id': '553',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': None,
   'lastAction': 'None'},
  {'id': '310297',
   'minute': '17',
   'result': 'Goal',
   'X': '0.885',
   'Y': '0.5',
   'xG': '0.7611688375473022',
   'player': 'Marcus Rashford',
   'h_a': 'h',
   'player_id': '556',
   'situation': 'Penalty',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': None,
   'lastAction': 'Standard'},
  {'id': '310298',
   'minute': '21',
   'result': 'BlockedShot',
   'X': '0.865999984741211',
   'Y': '0.6409999847412109',
   'xG': '0.08496686071157455',
   'player': 'Anthony Martial',
   'h_a': 'h',
   'player_id': '553',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Paul Pogba',
   'lastAction': 'HeadPass'},
  {'id': '310299',
   'minute': '29',
   'result': 'MissedShots',
   'X': '0.7719999694824219',
   'Y': '0.63',
   'xG': '0.031568706035614014',
   'player': 'Anthony Martial',
   'h_a': 'h',
   'player_id': '553',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Jesse Lingard',
   'lastAction': 'Pass'},
  {'id': '310300',
   'minute': '31',
   'result': 'MissedShots',
   'X': '0.92',
   'Y': '0.435',
   'xG': '0.05561231076717377',
   'player': 'Paul Pogba',
   'h_a': 'h',
   'player_id': '1740',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'Head',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Anthony Martial',
   'lastAction': 'Aerial'},
  {'id': '310309',
   'minute': '53',
   'result': 'BlockedShot',
   'X': '0.815',
   'Y': '0.519000015258789',
   'xG': '0.057843536138534546',
   'player': 'Marcus Rashford',
   'h_a': 'h',
   'player_id': '556',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Harry Maguire',
   'lastAction': 'Pass'},
  {'id': '310311',
   'minute': '64',
   'result': 'Goal',
   'X': '0.9619999694824218',
   'Y': '0.5329999923706055',
   'xG': '0.6098462343215942',
   'player': 'Anthony Martial',
   'h_a': 'h',
   'player_id': '553',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Andreas Pereira',
   'lastAction': 'Cross'},
  {'id': '310312',
   'minute': '66',
   'result': 'Goal',
   'X': '0.8219999694824218',
   'Y': '0.4759999847412109',
   'xG': '0.37157538533210754',
   'player': 'Marcus Rashford',
   'h_a': 'h',
   'player_id': '556',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Paul Pogba',
   'lastAction': 'Throughball'},
  {'id': '310315',
   'minute': '79',
   'result': 'BlockedShot',
   'X': '0.84',
   'Y': '0.6020000076293945',
   'xG': '0.11250953376293182',
   'player': 'Marcus Rashford',
   'h_a': 'h',
   'player_id': '556',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Paul Pogba',
   'lastAction': 'Pass'},
  {'id': '310316',
   'minute': '80',
   'result': 'Goal',
   'X': '0.88',
   'Y': '0.41200000762939454',
   'xG': '0.13182462751865387',
   'player': 'Daniel James',
   'h_a': 'h',
   'player_id': '5595',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Paul Pogba',
   'lastAction': 'Pass'},
  {'id': '310318',
   'minute': '86',
   'result': 'BlockedShot',
   'X': '0.8669999694824219',
   'Y': '0.47299999237060547',
   'xG': '0.11503136157989502',
   'player': 'Mason Greenwood',
   'h_a': 'h',
   'player_id': '7490',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Aaron Wan-Bissaka',
   'lastAction': 'Cross'}],
 'a': [{'id': '310293',
   'minute': '3',
   'result': 'ShotOnPost',
   'X': '0.835999984741211',
   'Y': '0.38599998474121094',
   'xG': '0.03392893448472023',
   'player': 'Tammy Abraham',
   'h_a': 'a',
   'player_id': '702',
   'situation': 'FromCorner',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Mateo Kovacic',
   'lastAction': 'BallTouch'},
  {'id': '310294',
   'minute': '3',
   'result': 'MissedShots',
   'X': '0.7630000305175781',
   'Y': '0.64',
   'xG': '0.022202739492058754',
   'player': 'Pedro',
   'h_a': 'a',
   'player_id': '687',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Mateo Kovacic',
   'lastAction': 'Pass'},
  {'id': '310296',
   'minute': '11',
   'result': 'SavedShot',
   'X': '0.7390000152587891',
   'Y': '0.44299999237060544',
   'xG': '0.021734273061156273',
   'player': 'Mason Mount',
   'h_a': 'a',
   'player_id': '7768',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Pedro',
   'lastAction': 'Pass'},
  {'id': '310301',
   'minute': '34',
   'result': 'MissedShots',
   'X': '0.7269999694824218',
   'Y': '0.46599998474121096',
   'xG': '0.01919102855026722',
   'player': 'Ross Barkley',
   'h_a': 'a',
   'player_id': '592',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Pedro',
   'lastAction': 'Pass'},
  {'id': '310302',
   'minute': '35',
   'result': 'SavedShot',
   'X': '0.7559999847412109',
   'Y': '0.355',
   'xG': '0.0167279914021492',
   'player': 'Pedro',
   'h_a': 'a',
   'player_id': '687',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'César Azpilicueta',
   'lastAction': 'BallRecovery'},
  {'id': '310303',
   'minute': '38',
   'result': 'SavedShot',
   'X': '0.9119999694824219',
   'Y': '0.6659999847412109',
   'xG': '0.06690332293510437',
   'player': 'Ross Barkley',
   'h_a': 'a',
   'player_id': '592',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Pedro',
   'lastAction': 'Pass'},
  {'id': '310304',
   'minute': '38',
   'result': 'BlockedShot',
   'X': '0.799000015258789',
   'Y': '0.4759999847412109',
   'xG': '0.039041295647621155',
   'player': 'Jorginho',
   'h_a': 'a',
   'player_id': '1389',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': None,
   'lastAction': 'Rebound'},
  {'id': '310305',
   'minute': '39',
   'result': 'ShotOnPost',
   'X': '0.9390000152587891',
   'Y': '0.6759999847412109',
   'xG': '0.05201100930571556',
   'player': 'Emerson',
   'h_a': 'a',
   'player_id': '1245',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': None,
   'lastAction': 'None'},
  {'id': '310306',
   'minute': '45',
   'result': 'MissedShots',
   'X': '0.92',
   'Y': '0.524000015258789',
   'xG': '0.043574485927820206',
   'player': 'Kurt Zouma',
   'h_a': 'a',
   'player_id': '935',
   'situation': 'FromCorner',
   'season': '2019',
   'shotType': 'Head',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Mason Mount',
   'lastAction': 'Aerial'},
  {'id': '310307',
   'minute': '50',
   'result': 'BlockedShot',
   'X': '0.7730000305175782',
   'Y': '0.37099998474121093',
   'xG': '0.024473881348967552',
   'player': 'Pedro',
   'h_a': 'a',
   'player_id': '687',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Ross Barkley',
   'lastAction': 'Pass'},
  {'id': '310308',
   'minute': '51',
   'result': 'BlockedShot',
   'X': '0.9330000305175781',
   'Y': '0.48700000762939455',
   'xG': '0.0619838684797287',
   'player': 'Tammy Abraham',
   'h_a': 'a',
   'player_id': '702',
   'situation': 'FromCorner',
   'season': '2019',
   'shotType': 'Head',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Mason Mount',
   'lastAction': 'Aerial'},
  {'id': '310310',
   'minute': '55',
   'result': 'SavedShot',
   'X': '0.84',
   'Y': '0.715',
   'xG': '0.24400267004966736',
   'player': 'Emerson',
   'h_a': 'a',
   'player_id': '1245',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': None,
   'lastAction': 'Aerial'},
  {'id': '310313',
   'minute': '69',
   'result': 'MissedShots',
   'X': '0.9119999694824219',
   'Y': '0.42900001525878906',
   'xG': '0.029906723648309708',
   'player': 'Kurt Zouma',
   'h_a': 'a',
   'player_id': '935',
   'situation': 'SetPiece',
   'season': '2019',
   'shotType': 'Head',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Mason Mount',
   'lastAction': 'Aerial'},
  {'id': '310314',
   'minute': '70',
   'result': 'SavedShot',
   'X': '0.8290000152587891',
   'Y': '0.28299999237060547',
   'xG': '0.02788177877664566',
   'player': 'Emerson',
   'h_a': 'a',
   'player_id': '1245',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Pedro',
   'lastAction': 'Pass'},
  {'id': '310317',
   'minute': '84',
   'result': 'SavedShot',
   'X': '0.7819999694824219',
   'Y': '0.6480000305175782',
   'xG': '0.02486059069633484',
   'player': 'Mason Mount',
   'h_a': 'a',
   'player_id': '7768',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Pedro',
   'lastAction': 'Pass'},
  {'id': '310319',
   'minute': '87',
   'result': 'BlockedShot',
   'X': '0.8209999847412109',
   'Y': '0.6430000305175781',
   'xG': '0.04654185473918915',
   'player': 'Christian Pulisic',
   'h_a': 'a',
   'player_id': '2662',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'RightFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Mason Mount',
   'lastAction': 'Pass'},
  {'id': '310320',
   'minute': '88',
   'result': 'MissedShots',
   'X': '0.9569999694824218',
   'Y': '0.5570000076293945',
   'xG': '0.27949726581573486',
   'player': 'Pedro',
   'h_a': 'a',
   'player_id': '687',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'OtherBodyPart',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Emerson',
   'lastAction': 'Cross'},
  {'id': '310321',
   'minute': '93',
   'result': 'SavedShot',
   'X': '0.850999984741211',
   'Y': '0.7',
   'xG': '0.043492574244737625',
   'player': 'Emerson',
   'h_a': 'a',
   'player_id': '1245',
   'situation': 'OpenPlay',
   'season': '2019',
   'shotType': 'LeftFoot',
   'match_id': '11652',
   'h_team': 'Manchester United',
   'a_team': 'Chelsea',
   'h_goals': '4',
   'a_goals': '0',
   'date': '2019-08-11 16:30:00',
   'player_assisted': 'Christian Pulisic',
   'lastAction': 'Pass'}]}

I essentially want to get all the X and Y values from each list ('h' and 'a') so I can use them as coordinates to plot with ax.annotate in matplotlib.

Any help would be appreciated!

Thanks.

4
  • Please edit the full error message into our question. Commented Apr 18, 2020 at 19:31
  • ' is not valid JSON syntax, it should be " : ) Commented Apr 18, 2020 at 19:32
  • 3
    print([(k['X'], k['Y']) for k in data['h']]) Commented Apr 18, 2020 at 19:33
  • @CavinDsouza That worked! Thanks so much! :-) Commented Apr 18, 2020 at 19:41

2 Answers 2

2

data['h'] is a list of dicts

Try something like this

x_y = [(d['X'], d['Y']) for d in data['h']]
Sign up to request clarification or add additional context in comments.

Comments

0

Above answer is perfectly fine. I am just answering this to tell that you don't need to do f.close() when opening a file with with open() construct!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.