Conditional logic for processing Airtable records in Python

I have three entries from Airtable that I need to handle using a for loop in Python. My goal is to check if the value in the ‘check’ field is ‘Update2’. If it is, I will perform one action; otherwise, I will do something different.

Here are the records I’m working with:

{
    'createdTime': '2022-11-09T15:57:28.000Z',
    'fields': {
        'Last Modified': '2022-11-10T00:22:31.000Z',
        'Name': 'Daniel',
        'Status': 'Todo',
        'check': 'update2'
    },
    'id': 'recbvBuBBrgWO98pZ'
}
{
    'createdTime': '2022-11-09T16:58:15.000Z',
    'fields': {
        'Last Modified': '2022-11-10T00:22:32.000Z',
        'Name': 'Claudia',
        'Status': 'In progress',
        'check': 'update2'
    },
    'id': 'reck3BB7lOVKG0cPI'
}
{
    'createdTime': '2022-11-09T15:57:28.000Z',
    'fields': {
        'Last Modified': '2022-11-10T00:22:32.000Z',
        'Name': 'Isabella',
        'Status': 'Done',
        'check': 'update2'
    },
    'id': 'recveGd8w9ukxLkk9'
}

I’m looking for guidance on how to implement this loop correctly.

You’re checking for ‘Update2’ but your sample data shows ‘update2’ in lowercase. This case sensitivity issue got me when I first worked with Airtable data. Either normalize the case or use a case-insensitive comparison. Here’s what worked for me:

for record in records:
    check_value = record['fields'].get('check', '').lower()
    if check_value == 'update2':
        # Your update action here
        print(f"Processing update for {record['fields']['Name']}")
    else:
        # Alternative action
        print(f"Different action for {record['fields']['Name']}")

Using .get('check', '') prevents KeyError exceptions if the field’s missing from some records - happens more often than you’d expect with Airtable imports. The .lower() method keeps comparison consistent no matter how the data was entered.

Quick heads up - add a check for missing ‘fields’ data. Airtable sometimes returns incomplete records, so your script will crash if ‘fields’ doesn’t exist or the ‘check’ field is missing. Try if 'fields' in record and 'check' in record['fields']: before grabbing the check value.