I was trying to grab the data from youtube using the API, hopefully, I did, but While trying to parse the file I got an error, string indices must be integers.
Following is the error which I am facing...
TypeError
Traceback (most recent call last)
<ipython-input-48-213e690c5b60> in <module>----> 1 response['items'][0]['id']['videoId']['snippet']['title']
TypeError: string indices must be integers
Actually, I was trying to grab the first video from the channel So I put response['items'][0]
, I got that easily... but when was trying to parse the Video_ID
and Title
of that video I am getting this error.
However, when I am executing them separately, I am getting the output.
OUTPUT when executed separately:
response['items'][0]['id']['videoId']
'gzJGqML4j5k'
response['items'][0]['snippet']['title']
'Roles And Responsibilities Of An AI Engineer'
Output when executed together:
response['items'][0]['id']['videoId']['snippet']['title']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-213e690c5b60> in <module>
----> 1 response['items'][0]['id']['videoId']['snippet']['title']
TypeError: string indices must be integers
Can anyone help me out and tell me how to get this output in one single command.
As you get in comments - you can't get two values from JSON
/dictionary
in one step.
You have to get every value separatelly and use both in one print()
More readable:
val1 = response['items'][0]['id']['videoId']
val2 = response['items'][0]['snippet']['title']
print(val1, val2)
The same but in one line:
print(response['items'][0]['id']['videoId'], response['items'][0]['snippet']['title'])
Or short version which is still readable:
item = response['items'][0]
print(item['id']['videoId'], item['snippet']['title'])
and it works with for
-loop
for item in response['items']:
print(item['id']['videoId'], item['snippet']['title'])