Sep-02-2017, 04:16 PM
End Goal: Pull users from web site via API and list only certain fields in Python.
The website has an area that we can put additional information about a user i.e Department. The department isn't a part of the user_data and is in another location. As of now, I have only figured out how to pull user_data keys and print it out. Now I must combine these additional fields.
So when I use this type of url;
"https://api-calling.com/api/users/?includes[]=custom_fields" - I'm pulling the users and tacking on the additional fields I need to view.
The json output looks like this when it's called for example;
"linked": {"custom_fields":[{"id":"4", "name":"Department"}],
"custom_field_values":[{"id":"3204","value":"Money",
"links":{"custom_field":{"id":"4","type":"custom_fields"}}}]}},"users":[{"id":"400","full_name":"Uncle Sam"}]
I would like to structure this output in Python
ID = 400
Name = Uncle Sam
Department: Money
It seems the custom_fields is defined on the website with an ID with the name of the field(in this case Department) . Then when a user is called it looks for that id 4 for Department and fills in a new id in custom_field_values based on the input Money. Then it ties it altogether with the user ID.
The website has an area that we can put additional information about a user i.e Department. The department isn't a part of the user_data and is in another location. As of now, I have only figured out how to pull user_data keys and print it out. Now I must combine these additional fields.
So when I use this type of url;
"https://api-calling.com/api/users/?includes[]=custom_fields" - I'm pulling the users and tacking on the additional fields I need to view.
The json output looks like this when it's called for example;
"linked": {"custom_fields":[{"id":"4", "name":"Department"}],
"custom_field_values":[{"id":"3204","value":"Money",
"links":{"custom_field":{"id":"4","type":"custom_fields"}}}]}},"users":[{"id":"400","full_name":"Uncle Sam"}]
I would like to structure this output in Python
ID = 400
Name = Uncle Sam
Department: Money
It seems the custom_fields is defined on the website with an ID with the name of the field(in this case Department) . Then when a user is called it looks for that id 4 for Department and fills in a new id in custom_field_values based on the input Money. Then it ties it altogether with the user ID.
import requests url = "https://apiwebsite.com/api/users?includes[]=custom_fields" headers = { 'accept': "application/json", 'content-type': "application/json", 'authorization': "Basic myapikey", 'cache-control': "no-cache", } b_url = url r = requests.get(b_url, headers=headers) data = r.json() user_ids = [] for user in data['users']: user_ids.append(user['id']) for foo in user_ids: #new_data = h.json() print(new_data.keys()) for user_data in new_data['users']: print(user_data.keys()) print("User ID is: " + user_data['id']) print("User Full Name is: " + user_data['full_name']) print("HR ID is: " + user_data['hr_id'])