# Getting python error "KeyError" when asking for first\_name or last\_name from users

**URL:** <https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754>\
**Category:** API and Webhooks\
**Created:** [October 21, 2022, 6:26pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754 "2022-10-21T18:26:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsullivan](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/rsullivan/32/42430_2.png) [@rsullivan](https://devforum.zoom.us/u/rsullivan)\
**Post date:** [October 21, 2022, 6:26pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/1 "2022-10-21T18:26:29Z")

</div>

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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [October 22, 2022, 12:10am UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/2 "2022-10-22T00:10:19Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![rsullivan](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/rsullivan/32/42430_2.png) [@rsullivan](https://devforum.zoom.us/u/rsullivan)\
**Post date:** [October 24, 2022, 12:09pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/4 "2022-10-24T12:09:35Z")

</div>

If I try and pull out first\_name and last\_name I get a KeyError. Any other piece of information works.

---

<div class="post-metadata">

**Author:** ![rsullivan](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/rsullivan/32/42430_2.png) [@rsullivan](https://devforum.zoom.us/u/rsullivan)\
**Post date:** [October 24, 2022, 12:17pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/5 "2022-10-24T12:17:48Z")

</div>

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [October 25, 2022, 3:17pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/6 "2022-10-25T15:17:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rsullivan](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/rsullivan/32/42430_2.png) [@rsullivan](https://devforum.zoom.us/u/rsullivan)\
**Post date:** [November 8, 2022, 3:32pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/8 "2022-11-08T15:32:34Z")

</div>

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:

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

```

The result is

```auto
['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!

---

<div class="post-metadata">

**Author:** ![donte.zoom](https://sea2.discourse-cdn.com/flex016/user_avatar/devforum.zoom.us/donte.zoom/32/24412_2.png) [@donte.zoom](https://devforum.zoom.us/u/donte.zoom)\
**Post date:** [November 9, 2022, 11:48pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/9 "2022-11-09T23:48:32Z")

</div>

@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”.

> [@rsullivan](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/zoomdeveloper/original/3X/6/1/614bdd549b610bbaa46ff934617683a02bdaa03c.png) [@system](https://devforum.zoom.us/u/system)\
**Post date:** [November 13, 2023, 3:25pm UTC](https://devforum.zoom.us/t/getting-python-error-keyerror-when-asking-for-first-name-or-last-name-from-users/77754/10 "2023-11-13T15:25:09Z")

</div>

Closed on November 13, 2023, 3:25pm UTC

This topic was automatically closed 368 days after the last reply. New replies are no longer allowed.
