Python conditional logic for processing Airtable data - working with JSON structure

I have some data coming from Airtable that looks like JSON format. I need to create a loop that goes through each record and checks a specific field value. Here’s what my data looks like:

[
    {
        'createdTime': '2022-12-15T10:30:45.000Z',
        'fields': {
            'Last Modified': '2022-12-16T09:15:20.000Z',
            'Name': 'Sarah',
            'Status': 'Active',
            'flag': 'process_me'
        },
        'id': 'recABC123XYZ456'
    },
    {
        'createdTime': '2022-12-15T11:45:30.000Z',
        'fields': {
            'Last Modified': '2022-12-16T09:15:21.000Z',
            'Name': 'Michael',
            'Status': 'Pending',
            'flag': 'process_me'
        },
        'id': 'recDEF789GHI012'
    },
    {
        'createdTime': '2022-12-15T12:20:15.000Z',
        'fields': {
            'Last Modified': '2022-12-16T09:15:22.000Z',
            'Name': 'Jennifer',
            'Status': 'Complete',
            'flag': 'skip_this'
        },
        'id': 'recJKL345MNO678'
    }
]

I want to iterate through these records and check if the ‘flag’ field equals ‘process_me’. If it does, I need to perform one action, otherwise do something different. How can I structure this conditional logic properly in Python?

I’ve been working with Airtable’s API and found batch processing with filtering works great here. Instead of checking records one by one, filter first:

processable_records = [record for record in airtable_data if record.get('fields', {}).get('flag') == 'process_me']
skippable_records = [record for record in airtable_data if record.get('fields', {}).get('flag') != 'process_me']

# Process all flagged records
for record in processable_records:
    # Your processing logic
    record_id = record['id']
    name = record['fields']['Name']
    # Handle processing

# Handle skipped records separately if needed
for record in skippable_records:
    # Log or handle skipped items
    pass

This really helps with larger datasets - you can batch process records by flag status. I’ve seen solid performance gains with hundreds of records, especially when the processing logic gets complex or hits external APIs.

I’ve hit the same issues with Airtable data processing. What caught me off guard was records where the ‘flag’ field just doesn’t exist. Your approach looks good, but definitely add error handling for field access:

for record in airtable_data:
    try:
        fields = record.get('fields', {})
        flag_status = fields.get('flag', 'default_value')
        
        if flag_status == 'process_me':
            # Process the record
            name = fields.get('Name', 'Unknown')
            status = fields.get('Status', 'No Status')
            # Your processing logic here
        else:
            # Skip or handle differently
            continue
            
    except KeyError as e:
        print(f"Missing key in record {record.get('id', 'unknown')}: {e}")
        continue

This has saved me from production errors when Airtable’s schema changes or records have inconsistent fields. API data with nested dictionaries can be a pain.

Here’s how I handle this:

for record in your_data:
    flag_value = record['fields'].get('flag', '')
    
    if flag_value == 'process_me':
        # Do your processing here
        print(f"Processing {record['fields']['Name']}")
        # Add your actual processing logic
    else:
        # Handle other cases
        print(f"Skipping {record['fields']['Name']}")
        # Add your skip logic

I use .get() for dictionary keys since Airtable records sometimes have missing fields. Saves you from KeyError exceptions.

Want cleaner code? Split the processing logic:

def process_record(record):
    # Your processing logic here
    pass

def skip_record(record):
    # Your skip logic here
    pass

for record in your_data:
    if record['fields'].get('flag') == 'process_me':
        process_record(record)
    else:
        skip_record(record)

Much easier to maintain when things get complicated.