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

问题描述 投票:0回答:2

我有一个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\":\"[email protected]\",\"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.

UPDATE这是实际编辑器的打印屏幕。我真的很困惑这里可能出什么问题:enter image description here

javascript json zapier
2个回答
0
投票

效果很好,我认为您的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\":\"[email protected]\",\"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)

0
投票

David,来自Zapier Platform团队。

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

=”

=”

我通过以下命令发送了此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
}

这应该有助于您朝正确的方向前进。

© www.soinside.com 2019 - 2024. All rights reserved.