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

隐身守侯 提交于 2020-06-18 02:37:33

问题


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]};

回答1:


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)



来源:https://stackoverflow.com/questions/55699288/is-there-a-simpler-way-to-extract-parse-nested-object-values-that-is-not-fixed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!