Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help With API Output
#1
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.

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'])
Reply
#2
Anyone have a pointer?
Reply
#3
Ok, so after messing around, I have managed to kind of solve some of the issues I was facing. However, now, it's calling the last key only. How can I print out Manager First Name...?
r = requests.get(url, headers=headers)

data = r.json()
print(data)
#custom_fields = {'0': 'Department', '1': 'Position', '2': 'Manager Id', '3': 'Location', '4': 'Manager First Name', '5': 'Manager Last Name'}

for custom in data['linked']['custom_fields']:
    print(custom.keys())


for values in data['linked']['custom_field_values']:
    print(values.keys())


print(custom)
print(values)
#print(values['id'])


print(custom['name'] + " is:", values['value'])
Output:
dict_keys(['id', 'name'])
dict_keys(['id', 'value', 'links'])
dict_keys(['id', 'value', 'links'])
dict_keys(['id', 'value', 'links'])
dict_keys(['id', 'value', 'links'])
dict_keys(['id', 'value', 'links'])
dict_keys(['id', 'value', 'links'])
{'id': '48', 'name': 'Manager Last Name'}
{'id': '7641', 'value': 'Dillon', 'links': {'custom_field': {'id': '48', 'type': 'custom_fields'}}}
Manager Last Name is: Dillon

Process finished with exit code 0
Reply
#4
custom and values only have meaning inside the for loop, as they are the value for each iteration of the loop.  After the for loop, they still have the value of the last iteration.  If that's not what you want, then you should use a variable to maintain values outside the for loop.

>>> thing = None
>>> values = {ndx: ndx**2 for ndx in range(5)}
>>> values
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> for value in values:
...   if value == 3:
...     thing = values[value]
...
>>> thing
9
>>> value
4
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020