How Can We Help?
Pure API: Extracting Person Dependents Using PythonPure API: Extracting Person Dependents Using Python
Introduction
This script allows you to extract data for a person’s dependents using the Pure API. The process involves reading a list of Person UUIDs from a CSV file, fetching the details of each person (such as their first and last name), retrieving dependent data for each person, and exporting dependent details to JSON files. The script handles various dependent types, such as Activities, Awards, Research Outputs, and more, by calling the relevant endpoints in the Pure API.
Pure API examples disclaimer
Disclaimer
• Proceed at your own risk. Running this script/function means you will not blame the author if this breaks your data.
• This script/function is provided AS IS without warranty of any kind. It is recommended to run them in a test-staging environment to assess the outcome and understand their functionality.
• Elsevier will not support or review any code that uses or is based on these examples. Nor will Elsevier support it in any other way.
• This script has been tested with Pure 5.31 but might not work for future versions of Pure.
• This script is developed for Python version 3.11 and have not been tested with any other version of Python.
• Please make sure any Pure server you run this script against, has been backed up prior to running this script, to insure you can return to a working Pure if things fail
Requirements
- Pure Admin Account
- Valid API Key
- API Documentation
- Python
Expertise Level
Knowledge of Python is required. See also Python API Requests: Fundamental Coding Examples
High-Level Process
- Read the listing of Person UUIDs from a CSV file.
- Fetch the Person details (first name, last name).
- Retrieve the dependent data for each person.
- Fetch the details of each dependent (e.g., Activity, Award, Prize, Research Output).
-
Export the dependent details to JSON file format for each dependent using the naming convention
person_firstname_lastname_systemName_dependentUUID.json
, organized into folders named after each person's UUID.
Toggle for Python Script code
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 10 09:07:04 2025
@author: mlr
"""
import requests
import csv
import json
import os
API_KEY = 'YourAPIKeyHere'
BASE_URL = 'https://YourPureURL/ws/api/'
ENDPOINTS = {
"Activity": "/activities/{uuid}",
"Application": "/applications/{uuid}",
"Award": "/awards/{uuid}",
"DataSet": "/data-sets/{uuid}",
"Equipment": "/equipment/{uuid}",
"Prize": "/prizes/{uuid}",
"Project": "/projects/{uuid}",
"ResearchOutput": "/research-outputs/{uuid}",
"StudentThesis": "/student-theses/{uuid}"
# double check your Pure API documentations, additional endpoint may be needed e.g. Press-media
}
def get_person_details(person_uuid):
"""Fetches the person's first and last name using the UUID."""
url = f"{BASE_URL}/persons/{person_uuid}"
headers = {
'accept': 'application/json',
'api-key': API_KEY
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
person_data = response.json()
first_name = person_data.get('name', {}).get('firstName', 'Unknown')
last_name = person_data.get('name', {}).get('lastName', 'Unknown')
return first_name, last_name
else:
print(f"Error fetching person details for UUID {person_uuid}")
return None, None
def fetch_person_dependents(person_uuid, first_name, last_name):
"""Fetches dependents of a person and saves details as JSON in a directory named after the person's UUID."""
url = f"{BASE_URL}/persons/{person_uuid}/dependents"
headers = {
'accept': 'application/json',
'api-key': API_KEY
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
dependents_data = response.json().get('items', [])
# Create a directory for the person based on UUID
person_dir = os.path.join(os.getcwd(), person_uuid)
os.makedirs(person_dir, exist_ok=True)
for dependent in dependents_data:
system_name = dependent.get('systemName', 'Unknown')
dependent_uuid = dependent.get('uuid', 'Unknown')
if system_name in ENDPOINTS:
dependent_url = BASE_URL + ENDPOINTS[system_name].format(uuid=dependent_uuid)
dependent_response = requests.get(dependent_url, headers=headers)
if dependent_response.status_code == 200:
dependent_details = dependent_response.json()
# File naming: firstname_lastname_systemName_dependentUUID.json
file_name = f"{first_name}_{last_name}_{system_name}_{dependent_uuid}.json"
file_path = os.path.join(person_dir, file_name)
# Save JSON file
with open(file_path, 'w', encoding='utf-8') as json_file:
json.dump(dependent_details, json_file, indent=4)
print(f"Saved data for {system_name} in {file_path}")
else:
print(f"Error fetching {system_name} details for UUID {dependent_uuid}")
else:
print(f"Skipping unsupported systemName: {system_name}")
else:
print(f"Error fetching dependents for person UUID {person_uuid}")
def main():
"""Reads 'person.csv', retrieves each person's name, and saves dependent data in a structured folder format."""
with open('person.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
person_uuid = row['uuid'].strip() # Ensure no leading/trailing spaces
if not person_uuid:
continue # Skip empty UUIDs
first_name, last_name = get_person_details(person_uuid)
if first_name and last_name:
fetch_person_dependents(person_uuid, first_name, last_name)
if __name__ == "__main__":
main()
Process Breakdown
- Reading Input (CSV):
- The script starts by reading a CSV file (
person.csv
), which contains a list of people identified by their uniqueuuid
. - For each person in the CSV, the script retrieves their
uuid
to fetch further details.
- The script starts by reading a CSV file (
- Fetching Person Details:
- For each
uuid
, the script sends aGET
request to the/persons/{uuid}
endpoint. - The API response contains the person's
firstName
andlastName
, which are extracted and stored for later use. - If the request is successful, the script continues; if not, an error message is printed, and the process moves to the next person.
- For each
- Fetching Dependent Data:
- The script then sends another
GET
request to the/persons/{uuid}/dependents
endpoint to retrieve a list of dependents associated with the person. - The dependents are returned as a list of items, each containing information about the dependent type (systemName) and its unique identifier (uuid).
- For each dependent, the script determines its type based on the
systemName
field. If the type matches any of the predefined types in the script (such asResearchOutput
,Prize
,Award
, etc.), the script proceeds to fetch additional details about that dependent.
- The script then sends another
- Fetching Dependent Details:
- The script then makes a
GET
request to the relevant endpoint based on thesystemName
of the dependent (e.g.,/research-outputs/{uuid}
,/projects/{uuid}
). - Each dependent's details are returned as JSON data, which is then saved to a separate file using the naming convention
person_firstname_lastname_systemName_dependentUUID.json.
-
JSON files for each person are saved inside a dedicated folder named after their UUID (
uuid
), making them easy to read and parse later.
- The script then makes a
- Handling Errors and Unsupported Dependents:
- If a
systemName
is encountered that is not part of the predefined list in the script, the dependent is skipped, and a message is logged indicating the unsupported type. - If any of the API requests for person details or dependent data fail, the script prints an error message and moves on to the next item.
- If a
- Output:
- For each dependent successfully fetched, a corresponding JSON file is created and saved with relevant details.
-
JSON files are organized within folders named after each person's UUID (
uuid
). -
The naming convention for each file is
person_firstname_lastname_systemName_dependentUUID.json
to ensure easy identification.
Customer Benefits
- Automation: This script fully automates the process of retrieving and storing dependent data associated with a person, saving manual effort.
- Dynamic: It supports multiple dependent types (such as Research Outputs, Projects, and Prizes, Activities, Award, Application, Dataset, Equipment, Project, Student Thesis) without requiring further customization for each type.
- Scalability: The script processes as many people from the CSV file as needed, handling multiple records in a single execution.
- Easy-to-Use Outputs: The JSON files are easy to parse and integrate into other systems or processes for further analysis or reporting.
Use Case Example:
If the customer has a list of people (identified by uuid
), they can simply provide that list in a CSV format. The script will automatically fetch the associated details and dependent data (e.g., publications, awards, projects) for each person and save the data in a structured JSON format, ready for further analysis or integration. Ref: 00978439
Updated at February 13, 2025