Python conditional logic for processing Airtable JSON data

I have some JSON data from Airtable that I need to process in Python. I want to create a loop that checks a specific field value and performs different actions based on what it finds.

Here’s an example of the data structure I’m working with:

data_records = [
    {'timestamp': '2022-12-15T10:30:45.000Z',
     'attributes': {'Updated': '2022-12-16T08:15:20.000Z',
                    'Employee': 'Sarah',
                    'Priority': 'High',
                    'action': 'process'},
     'record_id': 'rec1A2B3C4D5E6F7G'},
    {'timestamp': '2022-12-15T11:45:30.000Z',
     'attributes': {'Updated': '2022-12-16T09:30:15.000Z',
                    'Employee': 'Michael',
                    'Priority': 'Medium',
                    'action': 'process'},
     'record_id': 'rec2H3I4J5K6L7M8N'}
]

I need help writing a for loop that will check if the ‘action’ field equals ‘process’ and then execute different code blocks depending on the result. How can I iterate through these records and apply conditional logic based on the field values?

I’ve dealt with similar Airtable JSON setups before. Nested conditionals work great for this. Just loop through your records and grab the action field straight from the attributes dictionary:

for record in data_records:
    if record['attributes']['action'] == 'process':
        # Execute processing logic here
        print(f"Processing record {record['record_id']} for {record['attributes']['Employee']}")
        # Add your specific processing code
    else:
        # Handle non-process records
        print(f"Skipping record {record['record_id']}")

Watch out though - sometimes the ‘action’ field won’t exist in certain records and you’ll get a KeyError. I use record['attributes'].get('action', '') to handle missing fields. You can also throw in elif statements for Priority or Employee fields if you need them.