-1

I am new to Python and APIs. I have an API at work that I can access and get details for computers.
At this point I just want to try a very basic thing for practice and print Computer Name and IP Address.
Using a Python book I have with a chapter on API I am trying the following as a test and get 200 response and the length result = 10. So initial connection is working.

import requests

# Make an API call and store response
url = 'https://removed.jamfcloud.com/JSSResource/advancedcomputersearches/id/8'

headers = {
    'accept': 'application/json',
}

r = requests.get(url, headers=headers, auth=('Username','Password'))
print(f"Status code: {r.status_code}")

#Store API response in a variable
response_dict = r.json()

#Process Results
print(response_dict.keys())

repo_dicts = response_dict['advanced_computer_search']
print(f"Length of repo_dicts is: {len(repo_dicts)}")

At this point I just want to get 1 record from the result and show Computer Name and IP.
One thing to mention is the book is using some GitHub API for their examples but I am modifying it to use my own.
The book says to add the following code:

#Examine the first repository
repo_dict = repo_dicts[0]
print(f"Computer Name: {repo_dict['Computer Name']}")

That fails and says:
Traceback (most recent call last):
File "...\API_Test_Jamf_1.py", line 22, in
repo_dict = repo_dicts[0]
KeyError: 0

This is probably the most basic thing ever but I am stuck.
The API result in the web browser starts like this:

<advanced_computer_search>
<id>8</id>
<name>Test Pull</name>
<view_as>Standard Web Page</view_as>
<sort_1/>
<sort_2/>
<sort_3/>
<criteria>
<size>0</size>
</criteria>
<display_fields>
<size>102</size>
<display_field>
<name>Last Check-in</name>
</display_field>
<display_field>
<name>JSS Computer ID</name>
</display_field>
<display_field>
<name>Computer Name</name>
</display_field>
<display_field>
<name>IP Addresses</name>
</display_field>

What do I need to do next to get:

  1. First record and show Computer Name and IP
  2. All records to show same 2 fields?

Here is a snippet (beginning) of the result of:

print(response_dict)

{'advanced_computer_search': {'id': 8, 'name': 'InfraMI Pull', 'view_as': 'Standard Web Page', 'sort_1': '', 'sort_2': '', 'sort_3': '', 'criteria': [], 'display_fields': [{'name': 'Last Check-in'}, {'name': 'JSS Computer ID'}, {'name': 'Computer Name'}, {'name': 'IP Addresses'}, {'name': 'Local Admins'}, {'name': 'FileVault 2 Eligibility'}, {'name': 'Managed'}, {'name': 'Disable Automatic Login'}, {'name': 'Bootstrap Token Supported'}, {'name': 'Build Sequence'}, {'name': 'Build'}, {'name': 'Disk Encryption Configuration'}, {'name': 'Bar Code'}, {'name': 'Active Directory Status'}, {'name': 'AD Cert Expiration'}, {'name': 'AD Status'}, {'name': 'Enrolled via Automated Device Enrollment'}, {'name': 'Email Address'}, {'name': 'MAC Address'}, {'name': 'Disk Format'}, {'name': 'IP Address'}, {'name': 'MDM Capability'}, {'name': 'XProtect Definitions Version'}, {'name': 'Secure Token Users'}, {'name': 'External Boot Level'}, {'name': 'SMC Version'}, {'name': 'Number of Processors'}, {'name': 'Boot Drive Available MB'}, {'name': 'Required Passcode Length'}, {'name': 'UDID'}, {'name': 'Computer Azure Active Directory ID'}, {'name': 'Gatekeeper'}, {'name': 'Supervised'}, {'name': 'Root Account'}, {'name': 'Asset Tag'}, {'name': 'Conditional Access Inventory State'}, {'name': 'S.M.A.R.T. Status'}, {'name': 'Watermark'}, {'name': 'Activation Lock Manageable'}, {'name': 'Total Number of Cores'}, {'name': 'Operating System Version'}, {'name': 'Username'}, {'name': 'FileVault 2 Status'}, {'name': 'User Approved MDM'}, {'name': 'Platform'}, {'name': 'Serial Number'}, {'name': 'Operating System'}, {'name': 'Operating System Build'}, {'name': 'FileVault 2 Institutional Key'}, {'name': 'Processor Type'}, {'name': 'Firmware Password Set'}, {'name': 'Last Enrollment'}, {'name': 'Boot Drive Percentage Full'}, {'name': 'Maximum Passcode Age'}, {'name': 'Office License'}, {'name': 'Password History'}, {'name': 'Boot ROM'}, {'name': 'Drive Capacity MB'}, {'name': 'Password Type'}, {'name': 'Bootstrap Token Escrowed'}, {'name': 'User Azure Active Directory ID'}, {'name': 'FileVault 2 Recovery Key Type'}, {'name': 'Screen Lock'}, {'name': 'Battery Capacity'}, {'name': 'Make'}, {'name': 'System Integrity Protection'}, {'name': 'Number of Available Updates'}, {'name': 'JAMF Binary Version'}, {'name': 'Last Inventory Update'}, {'name': 'Bluetooth Low Energy Capability'}, {'name': 'Managed By'}, {'name': 'Activation Lock Enabled'}, {'name': 'Last iCloud Backup'}, {'name': 'Available RAM Slots'}, {'name': 'NIC Speed'}, {'name': 'Remote Desktop Enabled'}, {'name': 'Optical Drive'}, {'name': 'Processor Speed MHz'}, {'name': 'Service Pack'}, {'name': 'Bus Speed MHz'}, {'name': 'Operating System Name'}, {'name': 'Master Password Set'}, {'name': 'Secure Boot Level'}, {'name': 'Minimum Number of Complex Characters'}, {'name': 'FileVault 2 Individual Key Validation'}, {'name': 'FileVault Status'}, {'name': 'Total RAM MB'}, {'name': 'Architecture Type'}, {'name': 'iTunes Store Account'}, {'name': 'Full Name'}, {'name': 'Core Storage Partition Scheme on Boot Partition'}, {'name': 'Model Identifier'}, {'name': 'Model'}, {'name': 'Last Reported IP Address'}, {'name': 'FileVault 2 User'}, {'name': 'Applications'}, {'name': 'Cached Packages'}, {'name': 'Available SWUs'}, {'name': 'Enrollment Method: PreStage enrollment'}, {'name': 'Plug-ins'}, {'name': 'Local User Accounts'}, {'name': 'Mapped Printers'}], 'computers': [{'name': 'HYDM002543514', 'udid': 'A082964F-EA4E-56BA-BD79-34554F542F58', 'id': 89, 'Computer_Name': 'HYDM002543514', 'Last_Check_in': '2019-11-18 13:25:24', 'JSS_Computer_ID': '89', 'IP_Addresses': '', etc....

Thanks

JetRocket11
  • 302
  • 4
  • 17

1 Answers1

1

Looking at the structure of the response_dict you posted (which, perhaps you should try to put in an editor that will format it nicely for you to help you parse it), the info you're looking for (computer name and IP) is in a list under "computers", which is a key under "advanced_computer_search" in your response. All the stuff before that (which is most of what you pasted here) I think is a description of the objects you're querying.

To access the list, you can call response_dict["advanced_computer_search"]["computers"].

To print the info from the first record:

name = response_dict["advanced_computer_search"]["computers"][0]["Computer_Name"]
ip = response_dict["advanced_computer_search"]["computers"][0]["IP_Addresses"]
print(f"Computer Name: {name}, IP: {ip}")

To do the same for all records:

for computer in response_dict["advanced_computer_search"]["computers"]:
    name = computer["Computer_Name"]
    ip = computer["IP_Addresses"]
    print(f"Computer Name: {name}, IP: {ip}")
Nano Tellez
  • 116
  • 6