Zapier Catch (Raw) Hook JSON 解析问题

Zapier Catch (Raw) Hook JSON parsing issue

我想使用 Zapier 和 Webhooks 在两个不同的 CRM(Clevertap 和 Intercom)之间配置同步。通常 Clevertap 将以下 JSON 发送到 webhook:

{
    "targetId": 1548328164, 
    "profiles": [
        {
            "event_properties": {
                "MSG-sms": true, 
                "MSG-push": true, 
                "businessRole": "EMPLOYEE", 
                "Mobile Number": "123123123123", 
                "Name": "Artem Hovtvianisa", 
                "Title": "Mr", 
                "Last Name": "Hovtvianisa", 
                "Gender": "M", 
                "Customer type": "Business Account Holder", 
                "MSG-email": true, 
                "First Name": "Artem", 
                "Last seen IP": "111.177.74.50", 
                "tz": "GMT+0200", 
                "International customer": "yes", 
                "isBusiness": true, 
                "Email": "xxxyyy@gmail.com", 
                "Identity": 15675
            }, 
            "objectId": "e32e4de3c1e84b2d9bab3707c92cd092", 
            "all_identities": [
                "15675", 
                "xxxyyy@gmail.com"
            ], 
            "email": "xxxyyy@gmail.com", 
            "identity": "15675"
        }
    ]
}

Zapier 提供两种类型的 catch webhook:常规和 Raw。

抓生钩

当我使用这种类型时,JSON 原始数据将得到妥善处理,在下一步(Zapier JS 代码应用程序)中,我能够像上面的示例一样传递正确的 JSON 数据。

然而,当我使用简单的 JS 代码解析 JSON 对象并获取 profiles[0] 数组值时,出现以下错误 "TypeError: Cannot read property '0' of undefined"

代码步骤中的 JS 代码:

var result = JSON.parse(JSON.stringify(inputData));
console.log(result.profiles[0]);
return result;

抓钩

如果我使用常规的 Catch Hook,以某种奇怪的方式挂钩解析数据,如下所示:

JSON.parse无法识别此结构。

请告知如何以正确的方式处理 Webhook Zapier 步骤以获取 profiles[0] 数组项值?

提前致谢!

来自 Zapier 平台团队的 David。你走在正确的轨道上!

Catch Raw Hook 是通往此处的必经之路。您的问题是数据以字符串形式传入,并且您在解析它之前对其进行了重新字符串化,这使您回到了原来的位置。更简单的版本:

JSON.stringify("asdf") // => "\"asdf\"", quotes in the string
JSON.parse("\"asdf\"") // => "asdf", the original string
"asdf".profiles // => undefined
undefined[0] // => error, cannot read property "0" of undefined

相反,只需解析它就可以了!

// all your variables are already in "inputData", so yours, 
// also named inputData, must be referenced explicitly. 
const result = JSON.parse(inputData.inputData); 
return {result: result, firstProfile: result.profiles[0]};