I want to extract data from a json file using Python 3 (PyCharm Edu) and import the url using urllib, I want to get all the items with the key 'count' and then perform a sum of all those values. But I am getting this error, maybe the error is that the json file is made up of a list of dictionaries. This is the traceback:
num = item['count']
TypeError: string indices must be integers
This is the link of the json file I am working with: jsonfile
import urllib.request, urllib.parse, urllib.error
import json
import ssl
# SSL Certificate Errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input("Enter url: ")
print("Retrieving:", url)
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read()
print("Retrieved ", len(data), "characters")
info = json.loads(data)
print('User count:', len(info))
theList = list()
print(['comments'])
for item in info:
num = item['count']
theList.append(int(num))
Sum = sum(theList)
print("Sum: ", Sum)
Your JSON looks like this:
And this is therefore what you receive in the variable
info
. We therefore see that this variable is a dictionary with two keys, "note" and "comments", with the key "comments" containing a list with more dictionaries.You have this loop:
Since we just saw that it
info
's the full dictionary, you're iterating through that dictionary. When you iterate through a dictionary what you get are its keys, so this loop of yours will execute twice, and the variableitem
will take the value"note"
on the first iteration and"comments"
on the second iteration. That is, at each iterationitem
it is a string .That's why when you then try to do
item['count']
you get an error. Beingitem
a string it doesn't make sense to access['count']
. If anything it would make sense to access one of your characters with something likeitem[0]
eg. That's why the error tells you that string indices must be integers .Actually you wanted to iterate through the elements of the list that is inside
"comments"
. There it would make sense what you do because in that caseitem
, in each iteration, it would contain one of the dictionaries that have a field"name"
and another"count"
.Therefore, it is enough in principle to change your loop for this other one: