Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and JSON - TypeError list indices must be integers not str

Tags:

python

json

I am learning to use Python and APIs (specifically, this World Cup API, http://www.kimonolabs.com/worldcup/explorer)

The JSON data looks like this:

[
  {
    "firstName": "Nicolas Alexis Julio",
    "lastName": "N'Koulou N'Doubena",
    "nickname": "N. N'Koulou",
    "assists": 0,
    "clubId": "5AF524A1-830C-4D75-8C54-2D0BA1F9BE33",
    "teamId": "DF25ABB8-37EB-4C2A-8B6C-BDA53BF5A74D",
    "id": "D9AD1E6D-4253-4B88-BB78-0F43E02AF016",
    "type": "Player"
  },

 {
    "firstName": "Alexandre Dimitri",
    "lastName": "Song-Billong",
    "nickname": "A. Song",
    "clubId": "35BCEEAF-37D3-4685-83C4-DDCA504E0653",
    "teamId": "DF25ABB8-37EB-4C2A-8B6C-BDA53BF5A74D",
    "id": "A84540B7-37B6-416F-8C4D-8EAD55D113D9",
    "type": "Player"
  },
   ]

I am simply trying to print all of the firstNames in this API. Here's what I have:

import urllib2
import json

url = "http://worldcup.kimonolabs.com/api/players?apikey=xxx"
json_obj = urllib2.urlopen(url).read
readable_json = json.dumps(json_obj)
playerstuff = readable_json['firstName']
for i in playerstuff:
    print i['firstName']

But when I run it, I get the error "...line 8, in ...TypeError: list indices must be integers, not str"

I have looked around for solutions, but seem to find questions to more "in depth" API questions and I don't really understand it all yet, so any help or explanation as to what I need to do would be amazing. Thank you!

like image 817
user3718365 Avatar asked Jul 12 '14 01:07

user3718365


People also ask

How do you fix list indices must be integers or slices not float?

The Python "TypeError: list indices must be integers or slices, not float" occurs when we use a floating-point number to access a list at a specific index. To solve the error, convert the float to an integer, e.g. my_list[int(my_float)] .

Can a list index be a string?

These indexes are always defined using integers. You might declare a variable that has the index value of a list element. But if this variable does not have an integer value and instead has a string value, you will face an error called “TypeError: list indices must be integers or slices, not str”.

What does list indices must be integers or slices not list mean?

This type error occurs when indexing a list with anything other than integers or slices, as the error mentions. Usually, the most straightforward method for solving this problem is to convert any relevant values to integer type using the int() function.

How do I fix TypeError list indices must be integers or slices not tuple?

The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.


3 Answers

First of all, you should be using json.loads, not json.dumps. loads converts JSON source text to a Python value, while dumps goes the other way.

After you fix that, based on the JSON snippet at the top of your question, readable_json will be a list, and so readable_json['firstName'] is meaningless. The correct way to get the 'firstName' field of every element of a list is to eliminate the playerstuff = readable_json['firstName'] line and change for i in playerstuff: to for i in readable_json:.

like image 193
jwodder Avatar answered Nov 07 '22 23:11

jwodder


I solved changing

readable_json['firstName']

by

readable_json[0]['firstName']
like image 23
Hernan Ramirez Avatar answered Nov 08 '22 00:11

Hernan Ramirez


You can simplify your code down to

url = "http://worldcup.kimonolabs.com/api/players?apikey=xxx"
json_obj = urllib2.urlopen(url).read
player_json_list = json.loads(json_obj)
for player in readable_json_list:
    print player['firstName']

You were trying to access a list element using dictionary syntax. the equivalent of

foo = [1, 2, 3, 4]
foo["1"]

It can be confusing when you have lists of dictionaries and keeping the nesting in order.

like image 5
AlexLordThorsen Avatar answered Nov 08 '22 00:11

AlexLordThorsen