I am new to coding and I am struggling returning all userId in the json file. I have have tried:
print(['Employees'][0:3]['userId']
I get the following error:
TypeError: list indices must be integers or slices, not str
Any help would be greatly aprreciated
{
"Employees": [ { "userId": "krish", "jobTitle": "Developer", "firstName": "Krish", "lastName": "Lee", "employeeCode": "E1", "region": "CA", "phoneNumber": "123456", "emailAddress": "[email protected]" }, { "userId": "devid", "jobTitle": "Developer", "firstName": "Devid", "lastName": "Rome", "employeeCode": "E2", "region": "MA", "phoneNumber": "1111111", "emailAddress": "[email protected]" }, { "userId": "tin", "jobTitle": "Program Directory", "firstName": "tin", "lastName": "jonson", "employeeCode": "E3", "region": "FA", "phoneNumber": "2222222", "emailAddress": "[email protected]" } ] }
CodePudding user response:
You need a list comprehension for this:
user_ids = [employee['userId'] for employee in output['Employees'][0:3]]
CodePudding user response:
Found another user also struggling with the same issues and this post in combination to the helpful comment helped me resolve my issue . Issues iterating through JSON list in Python?