如何在Zapier触发器中JSON.parse一个数组? [英] How do I JSON.parse an array in a Zapier Trigger?

查看:197
本文介绍了如何在Zapier触发器中JSON.parse一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试JSON.解析数组数据"..我需要能够将数组作为根传递.

I am trying to JSON.parse the array "data." I need to be able to pass the array as the root.

{
  "data": [
    {
      "type": "name",
      "id": "123"
    }
  ]
}

响应应如下所示,仅包含对象. Zapier似乎不适用于数组.

The response should look like this containing only objects. Zapier doesn't seem to work well with arrays.

{
      "type": "name",
      "id": "123"
}

我不能使用简单的脚本来完成工作吗?

Shouldn't I be able to use a simple script to get the job done?

基本上,您将要覆盖post_poll方法( https ://zapier.com/developer/documentation/v2/scripting/#polling )中的脚本,以便您可以拦截API的响应.之后,您只需要返回一个具有所需值的新对象.无需返回:{"data":[{...},{...},]},您只需要返回data的值.像这样:

Essentially, you're going to want to override the post_poll method (https://zapier.com/developer/documentation/v2/scripting/#polling) in scripting so you can intercept the response of the API. After that, you just need to return a new object with the values you want. Instead of returning: {"data":[ {...}, {...}, ]}, you just need to return the value of data. Something like:

xyz_post_poll: function(bundle){
  var response = JSON.parse(bundle.response.content);
  return response.data || [];
}

推荐答案

是的,您可以使用简单的脚本,Javascript或Python.单击现有触发器和操作之间的 + ,然后添加一个操作,然后选择Zapier的Code作为应用.假设JSON是触发器的输出:

Yes, you can use a simple script, Javascript or Python. Click the + in between your existing Trigger and Action, and add an Action, choosing Code by Zapier as the app. Assuming your JSON is the output of your Trigger:

{
  "data": [
    {
      "type": "name",
      "id": "123"
    }
  ]
}

Zapier的代码将为您提供以下选项:

Code by Zapier would present you with these options:

由Zapier运行Javascript设置代码

如果对象数组data具有多个元素,则Zapier将这些对象中属性type的所有值显示为标记为 数据类型 ,并将属性id的所有值都标记为 Data ID 的数组.如果选择typeid作为要传递给Code应用程序的input对象的属性名称,那么您的代码获取的Javascript对象是:

If the array of objects data has more than one element, then Zapier presents all values for the property type in those objects as an array labelled Data Type and all values for property id as an array labelled Data ID. If you choose type and id as the property names for the input object to be passed to the Code app, then the Javascript object your code gets is this:

input = {
 type: [ "name", "name2", "name3" ],
 id: [ "123", "456", "789" ]
};

您的代码然后可以在传递给下一个Action之前,以任意方式转换数据.

Your code can then transform the data any way you want, before passing to the next Action.

扎比尔编写的代码

这篇关于如何在Zapier触发器中JSON.parse一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆