Getting python error "KeyError" when asking for first_name or last_name from users

In an effort to retrieve the email address, first, last, and type of the users on our account, I’m trying to use python to get the information via JSON.

I am able to get everything EXCEPT the first_name and last_name fields. Here is what I have been testing with so far:

for page in range(1, total_pages):
	url = url_address + "?page_number=" + str(page)
	user_data = requests.get(url=url, headers=headers).json()
	user_email = [(email['email']) for email in user_data['users']]
	user_fname = [(first_name['first_name']) for first_name in user_data['users']]
print(user_email, user_fname)

I know the loop isn’t finished, I’m just trying prints to see what I get at the moment. If I only ask for the email and type, it works perfectly. If I include either first_name or last_name I get the following error:

Traceback (most recent call last):
  File "~/Desktop/zoomscript.py", line 43, in <module>
    user_fname = [(first_name['first_name']) for first_name in user_data['users']]
  File "~/Desktop/zoomscript.py", line 43, in <listcomp>
    user_fname = [(first_name['first_name']) for first_name in user_data['users']]
KeyError: 'first_name'

I’m still learning how to use python so I’m sure I have to just be missing something, but I’m not sure what to do. All of the documentation points to being able to ask for first_name and last_name, but nothing I try seems to work.

Any ideas out there?

@rsullivan ,

Welcome to the Developer Forum – happy to help. Can you share which API endpoint you are leveraging and the response you are getting (Personal Identifiable Information (PII ) omitted)? Once received, I can work to get a working python function for you.

If I try and pull out first_name and last_name I get a KeyError. Any other piece of information works.

import json
import csv
import jwt
import requests
import http.client
from time import time

API_KEY = 'XXXXX'
API_SEC = 'XXXXX'

token = jwt.encode({'iss': API_KEY, 'exp': time() + 5000}, API_SEC, algorithm='HS256')

conn = http.client.HTTPSConnection("api.zoom.us")
headers = {'authorization': 'Bearer {}'.format(token)}
url_address = "https://api.zoom.us/v2/users?page_size=300"

r = requests.get(url=url_address, headers=headers).json()
total_pages = int(r['page_count']) + 1
all_entries = []

for page in range(1, total_pages):
	url = url_address + "?page_number=" + str(page)
	user_data = requests.get(url=url, headers=headers).json()
	user_email = [(email['email']) for email in user_data['users']]
	user_type = [(utype['type']) for utype in user_data['users']]
	all_entries.extend(user_email)
	data = all_entries
	page += 1
print(all_entries)

Okay, I will look into this and get back to you @rsullivan!

1 Like

I think I just figured out why I’m getting key errors when retrieving “first_name” and “last_name” from the records… Not every user in our account has entered their first and last names for their account.

My gut feeling is that when Zoom stores the user data it only adds the key if it has an actual value. So instead of having an empty value for first_name it just simply doesn’t put the first_name field into the database. So when I do the following:

for user in user_list['users']:
    personal_attributes = list(user.keys())
    print(personal_attributes)

The result is

['id', 'first_name', 'last_name', 'email', 'type', 'pmi', 'timezone', 'verified', 'dept', 'created_at', 'last_login_time', 'last_client_version', 'pic_url', 'group_ids', 'language', 'phone_number', 'status', 'role_id', 'employee_unique_id']
['id', 'email', 'type', 'pmi', 'timezone', 'verified', 'dept', 'created_at', 'last_login_time', 'last_client_version', 'language', 'status', 'role_id', 'employee_unique_id']
['id', 'email', 'type', 'pmi', 'timezone', 'verified', 'dept', 'created_at', 'last_login_time', 'last_client_version', 'language', 'status', 'role_id', 'employee_unique_id']
['id', 'first_name', 'last_name', 'email', 'type', 'pmi', 'timezone', 'verified', 'dept', 'created_at', 'last_login_time', 'last_client_version', 'pic_url', 'group_ids', 'language', 'phone_number', 'status', 'role_id', 'employee_unique_id']

And as you can see the dictionary doesn’t contain first_name or last_name for some of the records. This would make sense if I’m getting the KeyError when trying to retrieve this information.

How bizarre!

1 Like

@rsullivan ,
Ah… that sounds like one of those gotchas. To control for that edge case, you would want to check if the “first_name” and “last_name” exists. That should get you a list of users that entered their “first_name” and “last_name”.

It is my understanding if no value is entered, the API will return the Key with a value of an empty string. You can verify this by making the API call with a tool like a postman and inspecting the response.