I’m trying to read information from a CSV file and modify existing records in my Airtable database using Python. My CSV contains employee data that I need to sync with my Airtable table.
Here’s my current table structure:
employee_data = {
"records": [
{
"id": "rec123ABC",
"fields": {
"Name": "John",
"LastName": "Smith",
"Status": "Active",
"EmployeeID": "EMP001",
"Phone": "555-0123",
"Salary": 15000,
"WorkDays": 22,
"StartDate": "2022-01-15",
"Email": "[email protected]"
}
}
]
}
I only want to update the WorkDays field using EmployeeID as my lookup key. My CSV file structure is simple:
EmployeeID,WorkDays
EMP001,20
EMP002,18
I’ve managed to read the CSV data:
import csv
import json
employee_records = {}
with open(csv_file, "r") as file:
reader = csv.DictReader(file)
for index, row in enumerate(reader):
employee_records[index] = row
Then I convert it to JSON format:
processed_data = json.loads(json.dumps(employee_records, indent=2))
Now I’m stuck on transforming this data into the correct Airtable format for the PATCH request. I need help structuring the data properly before sending it to the API.
My target structure should be:
update_payload = {
"records": [
{
"fields": {
"EmployeeID": "EMP001",
"WorkDays": 20
}
}
]
}
I’m considering two approaches but not sure which is better. Any suggestions on how to properly format this data for the API call?