I am currently trying to parse some data from a get request response and I keep getting this error: "TypeError: list indices must be integers or slices, not str"
it would be great, if anybody could help me out what i am trying to solve. thank you so much in advance.
import requests
import json
class userList(APIView):
def get(self,request,format=None):
user_data = []
url = 'https://reqres.in/api/users?page=1'
try:
r = requests.get(url).json()
user = {
'id': r['data']['id'],
'email': r['data']['email']
}
user_data.append(user)
return Response({"user_data":user_data}, status=status.HTTP_200_OK)
{
data:{
0:{
'id' : 1
'email' : "[email protected]"
'first_name' : "George"
'last_name' : "Bluth"
'avatar' : "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
}
1:{
'id' : 2
'email' : "[email protected]"
'first_name' : "apdas"
'last_name' : "loanz"
'avatar' : "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
}
2:{
'id' : 3
'email' : "[email protected]"
'first_name' : "joan"
'last_name' : "homli"
'avatar' : "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
}
}
}
x[y]into it's own line so you can see which is causing the error, i suspect you needr[0]['data']['id']r['data']and check if it has alistinstead of adict. In a quick look here at your json content I think that the correct is user['data'][someIndex]['id']for example. You should be use something liker['data'][0]['id']