Zapier 代码:将字符串转换为数组 javascript

Zapier Code: Transform String to Array javascript

我有一个 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.

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

效果很好我认为你的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)

来自 Zapier 平台团队的 David。

很奇怪!我进行了快速抽查,我的工作按预期进行:

我使用以下命令发送了这个 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
}

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