Search code examples
arraysjsonzapier

Is there a simpler way to extract/parse nested object values that is not fixed?


I am trying to parse a specific nested object (Sign_UP_Appointment) from the results of a webhook using code by Zapier

Webhook Results

{
"entity":{
    "OPPORTUNITY_ID":24096201,
    "OPPORTUNITY_NAME":"Scott Williams ",
    "OPPORTUNITY_STATE":"OPEN",
    "RESPONSIBLE_USER_ID":1737942,
    "OWNER_USER_ID":1737942,
    "DATE_CREATED_UTC":"2019-04-15T17:02:11.567",
    "DATE_UPDATED_UTC":"2019-04-15T17:02:40.437",
    "VISIBLE_TO":"OWNER",
    "CUSTOMFIELDS":[
        {
            "CUSTOM_FIELD_ID":"Administration_Type__c",
            "FIELD_VALUE":"Summary Administration"
        },
        {
            "CUSTOM_FIELD_ID":"Initial_Appointment__c",
            "FIELD_VALUE":"2019-04-11T20:45:00"
        },
        {
            "CUSTOM_FIELD_ID":"Sign_Up_Appointment__c",
            "FIELD_VALUE":"2019-04-18T21:00:00"
        }
    ],
    "TAGS":[],
    "LINKS":[
        {
            "LINK_ID":205236388,
            "CONTACT_ID":287320999,
            "OPPORTUNITY_ID":24096201
        }
    ]
}
}

I want to return only the Custom Field (Sign_Up_Appointment__c). I tried the code below but the issue is on subsequent results the order changes. Is there way to filter out only the Sign_Up_Appointment__c object?

const result = JSON.parse(inputData.body);
return {result: result, SignUpDate: result.entity.CUSTOMFIELDS[3]};

Solution

  • David here, from the Zapier Platform team.

    Yep! You can use Array.find:

    const result = JSON.parse(inputData.body);
    return {
      result,
      SignUpDate: result.entity.CUSTOMFIELDS.find(
        f => f.CUSTOM_FIELD_ID === "Sign_Up_Appointment__c"
      )
    };
    

    SignUpDate will be undefined if there's not a field with that field_id in the array (I'm not sure how likely this is)