-
Notifications
You must be signed in to change notification settings - Fork 0
/
users_extract_logic.py
69 lines (59 loc) · 1.96 KB
/
users_extract_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import json
def extract_user_data(response):
user_data = {}
def is_user_data(obj):
# Check if object contains common user data keys
user_keys = {'displayName', 'givenName', 'mail', 'id'}
return all(key in obj for key in user_keys)
def traverse(obj):
# Recursively traverse the JSON object
nonlocal user_data
if isinstance(obj, dict):
if is_user_data(obj):
user_data = obj
else:
for value in obj.values():
traverse(value)
elif isinstance(obj, list):
for item in obj:
traverse(item)
traverse(response)
return user_data
# Example response data
response = {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
"value": [
{
"businessPhones": [],
"displayName": "Aakanksha Raina",
"givenName": "Aakanksha",
"jobTitle": "Null",
"mail": "aakanksha.raina@cymmetri.com",
"mobilePhone": "null",
"officeLocation": "null",
"preferredLanguage": "null",
"surname": "Raina",
"userPrincipalName": "aakanksha.raina@cymmetri.com",
"id": "c835ea75-707b-4e63-b4dc-b24b4b522a09"
},
{
"businessPhones": [],
"displayName": "Abhishek Ghante",
"givenName": "Abhishek",
"jobTitle": "null",
"mail": "abhishek.ghante@cymmetri.com",
"mobilePhone": "null",
"officeLocation": "null",
"preferredLanguage": "null",
"surname": "Ghante",
"userPrincipalName": "abhishek.ghante@cymmetri.com",
"id": "c39d5cf7-2d6c-4281-b58b-451125761c3d"
}
]
}
#json_data = response.json()
# Parse the user data from the response
user_data = extract_user_data(response)
# Print the extracted user data
print(type(user_data))
print(user_data)