【问题标题】:Zapier Code: Transform String to Array javascriptZapier 代码:将字符串转换为数组 javascript
【发布时间】:2020-02-14 13:39:34
【问题描述】:

我有一个以字符串格式接收数据的 webhook:

"{\"id\":\"2119813016789714851\",\"auth0_id\":\"auth0|5bbef2b54dac115c7a86684b\",\"title\":null,\"first_name\":\"Gary\",\"last_name\":\"Richard\",\"date_of_birth\":\"1994-04-10T00:00:00.000Z\",\"phone\":\"+44123456789\",\"company_id\":\"2119813365948745637\",\"status\":\"NEEDS_REVIEW\",\"email\":\"demo-1@lendflo.com\",\"agree_lendflo\":true,\"agree_authorized\":true,\"delete_director_id\":null,\"mail_change_code\":null,\"postcode\":\"ng72du\",\"address\":\"10 Faraday Road, Nottingham, Nottinghamshire\",\"country\":\"United Kingdom\",\"name\":\"fdsqfdsqfdsq\",\"company_house_no\":\"485245874\",\"main_contact_first_name\":\"Gary\",\"main_contact_last_name\":\"Richard\",\"registered_address\":\"fdsqfdsqfdsq\",\"trading_address\":null,\"website\":null,\"last_year_revenue\":\"123456\",\"registered_address_postcode\":\" LE1 6RP\",\"trading_address_postcode\":null,\"vat_number\":null,\"employee_count\":0,\"primary_user\":\"2119813016789714851\",\"company_status\":\"SIGNUP_INCOMPLETE\",\"companyIndustries\":[{\"sic\":\"62090\",\"label\":\"Other information technology service activities\"},{\"sic\":\"64992\",\"label\":\"Factoring\"}],\"stage\":\"dev\"}"

我需要将该字符串转换为 JSON 并将其放入一个数组中,以便它可以通过 zap 的后续操作准备好。

我正在尝试用这段代码来处理 javascript 中的代码模块

output = []
var data = JSON.parse(input.data)
output.push(data)

但我得到一个错误:

We had trouble sending your test through.
You must return a single object or array of objects.

更新 这是实际编辑器的打印屏幕。 我真的很困惑这里可能出什么问题:

【问题讨论】:

  • 您在此处显示的所有内容看起来都可以正常工作,它是正确转换为对象的有效 json。也许这个错误是错误的,它正在寻找单个数据对象而不是在一个数组中?
  • 我想这就是我在这里的原因,我不明白我做错了什么,根据他们的文档,这里是关于输入的内容:我们应该向您的代码提供哪些输入数据(作为字符串)通过设置为名为 inputData 的变量的对象?
  • 我已经几乎完全按照您的要求进行了设置,并且测试没有问题。

标签: javascript json zapier


【解决方案1】:

David 来自 Zapier 平台团队。

很奇怪!我做了一个快速的抽查,我的工作正常:

我使用以下命令发送了这个 webhook:

curl https://hooks.zapier.com/hooks/catch/USER_ID/HOOK_ID/ -XPOST -d '{"very": "cool"}'

我注意到在您的输入中,您的 json 似乎包含在另一组字符串中?我仔细检查了我的inputData.webhook 确实是一个字符串。也许在你这边验证一下?你也可以稍微简化一下代码:

return {
  inputType: typeof inputData.webhook, // should be "string"
  outputType: typeof JSON.parse(inputData.webhook), // should be "object"
  output: JSON.parse(inputData.webhook) // should be your actual data, split out
}

这应该可以帮助您找到正确的方向。

【讨论】:

    【解决方案2】:

    效果很好,我认为您的 input.data 输入不正确

    const data = "{\"id\":\"2119813016789714851\",\"auth0_id\":\"auth0|5bbef2b54dac115c7a86684b\",\"title\":null,\"first_name\":\"Gary\",\"last_name\":\"Richard\",\"date_of_birth\":\"1994-04-10T00:00:00.000Z\",\"phone\":\"+44123456789\",\"company_id\":\"2119813365948745637\",\"status\":\"NEEDS_REVIEW\",\"email\":\"demo-1@lendflo.com\",\"agree_lendflo\":true,\"agree_authorized\":true,\"delete_director_id\":null,\"mail_change_code\":null,\"postcode\":\"ng72du\",\"address\":\"10 Faraday Road, Nottingham, Nottinghamshire\",\"country\":\"United Kingdom\",\"name\":\"fdsqfdsqfdsq\",\"company_house_no\":\"485245874\",\"main_contact_first_name\":\"Gary\",\"main_contact_last_name\":\"Richard\",\"registered_address\":\"fdsqfdsqfdsq\",\"trading_address\":null,\"website\":null,\"last_year_revenue\":\"123456\",\"registered_address_postcode\":\" LE1 6RP\",\"trading_address_postcode\":null,\"vat_number\":null,\"employee_count\":0,\"primary_user\":\"2119813016789714851\",\"company_status\":\"SIGNUP_INCOMPLETE\",\"companyIndustries\":[{\"sic\":\"62090\",\"label\":\"Other information technology service activities\"},{\"sic\":\"64992\",\"label\":\"Factoring\"}],\"stage\":\"dev\"}"
    
    const output = []
    const json = JSON.parse(data)
    
    output.push(json)
    
    console.log(output)

    【讨论】:

    • 我再次检查过,在实际变量中重命名没有任何效果...请在打印屏幕中查看上面的更新
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2019-06-13
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    相关资源
    最近更新 更多