Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error json
#1
Greetings, in my code, I try to list only the parameters I need. In this case, they are face attributes and emotions. I still get a mistake. that string indexes must be integers


error list

return user['faceAttributes']['emotion']
TypeError: string indices must be integers

headers = {
    'Ocp-Apim-Subscription-Key': 'cccf20b44cb9445a8a4239ce324acfed',
    'Content-Type': 'application/octet-stream'
}
parameters = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,emotion'
}

def post_image(img_data):
    response = requests.post(BASE_URL, params=parameters,
                             headers=headers, data=img_data)
    try:
        return response.json()
    except:
        return None

def get_emotions(jsonData):
    j = json.dumps(jsonData)
    halo = json.loads(j)
    for user in halo:
        return user['faceAttributes']['emotion']
Reply
#2
Look at:
halo = json.loads(j)
print(halo)
# json.loads() should give back a python dictionary
print(type(halo))
for user in halo: will loop over key in dictionary,not both key and values.
It's now a string you try to get key from not a dictionary,just to make your error.
>>> s = 'foo'
>>> s['faceAttributes']
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: string indices must be integers

# So a string need integer,but this is not what you want
>>> s[0]
'f'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 588 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  json decoding error deneme2 10 3,400 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  python requests library .JSON() error mHosseinDS86 6 3,249 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,309 Nov-23-2022, 03:51 PM
Last Post: snippsat
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,882 Jan-11-2021, 07:52 PM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 9,951 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  empty json file error mcmxl22 1 9,984 Jun-17-2020, 10:20 AM
Last Post: buran
  json.dumps output error in python3 prayuktibid 2 2,614 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Reading JSON - error jmair 2 2,204 May-22-2019, 07:25 PM
Last Post: jmair
  Error message in Jupyter Notebook with json diet 4 5,491 Jun-17-2018, 08:32 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020