How to access nested API response data using dynamic input variable as object key in Zapier integration

I’m working on a custom Zapier integration and running into a problem with accessing nested data from my API response. The issue is that I need to get to an array that’s buried two levels deep in the response, and the key I need to access changes based on user input.

Basically, I want to do something like return apiData.field_collection[bundle.inputData.dynamicKey] but I can’t seem to make this work properly. The input variable doesn’t get recognized when I try to use it as a key.

Here’s my current API call setup:

const requestConfig = {
  url: `https://api.projectapp.com/v2/field_data.json?type=task&record_id=${bundle.inputData.record_id}&field_type=Assigned User`,
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${bundle.authData.token}`,
    'Accept': 'application/json'
  },
  params: {
    'record_id': bundle.inputData.record_id,
    'field_label': 'Assigned User'
  }
}

return z.request(requestConfig)
  .then((response) => {
    response.throwForStatus();
    const apiData = z.JSON.parse(response.content);
    
    return apiData.field_collection[bundle.inputData.dynamicKey];
  });

The API returns something like this:

{
  "887766543": {
    "editable": true,
    "record_type": "task",
    "workspace_id": 9988776,
    "record_id": 445566778,
    "modified_date": "2019-05-22T10:30:45-07:00",
    "data": [
      774433
    ],
    "readable_value": "John Smith",
    "modifier_id": "12345678",
    "field_id": "998877",
    "creation_date": "2019-05-10T09:15:30-08:00",
    "field_label": "Assigned User",
    "input_type": "single_select",
    "id": "887766543"
  }
}

I need to extract just the object with key “887766543” but this key changes for each request. The good news is I can pass this key as input data from the user. Any ideas on how to make this work?

Had the same problem with dynamic keys in Zapier. The previous answer’s right about the property path, but you’ll want error handling too since dynamic keys are unpredictable. After fixing the property access, wrap it in if (apiData[bundle.inputData.dynamicKey]) before returning. If your dynamicKey input’s undefined or empty, the whole thing fails silently. I usually add a fallback that returns the first available key’s data when the dynamic lookup fails - super helpful during testing when users don’t provide the right key format.

you’re accessing the wrong property. try return apiData[bundle.inputData.dynamicKey] instead of apiData.field_collection[bundle.inputData.dynamicKey]. your API response doesn’t have a field_collection wrapper - the dynamic key sits at the top level.

Yeah, bracket notation works, but I hit another issue with Zapier integrations like this. First, make sure your dynamicKey field is actually getting the right value. Add some debugging: z.console.log('Dynamic key:', bundle.inputData.dynamicKey); before your return statement. Sometimes Zapier’s UI messes up the input mapping and you get undefined or a completely different property name. Also check if users are adding quotes around the key value - I’ve seen that kill lookups even when the key exists. Once you’ve got a clean key value, apiData[bundle.inputData.dynamicKey] should work fine for accessing top-level objects.