2

I can't find a solution because the lack of understanding of programming. I have created multiple Python Scripts where I do API calls to get results, the results are converted to JSON.

I want to store two specific fields from the JSON result for each user in an array, so I then later can make a "search" based of a users input(field), match it to the array and send a post request later.

The fields I want is: 'email' and 'userid', so when I search for the input (email) I can retrieve the userid, and send the post call from a button click.

So I understand that I can retrieve either email or userid by using the teo first lines to print the values. But the issue is when i try to store it in the array:

users = response.json()

print(users['user'][1]['userid']) #get user id
print(users['user'][1]['email']) #get email

json_length = len(users['user']) #get amount of users

print(json_length)

user_list = []
for i in users:
    user_details = {"id":None, "email":None}
    user_details['id'] = users['userid'][i]
    user_details['email'] = users['email'][i]
    user_list.append(user_details)

print(user_list)

I get the following error:

Exception has occurred: TypeError list indices must be integers or slices, not str File "C:...test2.py", line 32, in user_details['id'] = users['user'][i]['userid']

The JSON (multiple fields are removed and renamed)

{
   "total":"2001",
   "totalcount":"2",
   "user":[
      {
         "userid":1,
         "name":{
            "firstname":"John",
            "lastname":"Doe (Inactive)"
         },
         "email":"[email protected]",
         "status":"Inactive",
         "organisation":{
            "orgname":"something",
            "orgid":1
         },

      },
      {
         "userid":2,
         "name":{
            "firstname":"Emma",
            "lastname":"Hart (Inactive)"
         },
         "email":"[email protected]",
         "status":"Inactive",
         "organisation":{
            "orgname":"otherthing",
            "orgid":2
         },
      }
   ]
}

Any help for pointing what I'm doing wrong or help?

2
  • Your error doesn't match the code you posted Commented May 6, 2021 at 13:00
  • My mistake! I tried adding the ['userid'] after the [i], but forgot to remove that when I ran the code again. Commented May 6, 2021 at 13:43

1 Answer 1

3

You're looping over the full JSON response keys, not the actual users list, then i would be each user object, so use that rather than the response again

Try

for i in users.get('user', []):
    ... 
    user_details['id'] = i['userid']
    user_details['email'] = i['email']

You can also build that list in one line

user_list = [ {"id":u["userid"], "email":u["email"]} for u in users.get('user', []) ]
print(user_list)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Finally able to store them - this will make the rest hopefully easier for a newbie like me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.