有没有更简单的方法来提取/解析未固定的嵌套对象值?

时间:2019-04-16 01:22:51

标签: arrays json zapier

我正在尝试使用Zapier的代码从Webhook的结果中解析特定的嵌套对象(Sign_UP_Appointment)

Webhook结果

{
"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
        }
    ]
}
}

我只想返回自定义字段(Sign_Up_Appointment__c)。我尝试了下面的代码,但问题在于顺序更改的后续结果。有没有办法只过滤出Sign_Up_Appointment__c对象?

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

1 个答案:

答案 0 :(得分:0)

Zapier Platform团队的David在这里。

是的!您可以使用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将是undefined,如果数组中没有与该field_id相同的字段(我不确定这有多大可能)