How do I JSON.parse an array in a Zapier Trigger?

2020-06-04 12:23发布

问题:

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"
    }
  ]
}

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?


EDIT:

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 || [];
}

回答1:

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"
    }
  ]
}

Code by Zapier would present you with these options:

Setup Code by Zapier Run 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" ]
};

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

Code by Zapier,



回答2:

I found that I needed to call JSON.parse() and JSON.stringify() in order to get this to work. Assume that my input gets putted in Zapier as (key,value) where the key = data and the value is:

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

My code:

output = {};

var obj = JSON.parse(input.data);

for (var i = 0; i < obj.length; i++) {
   output["myObject"+i] = JSON.stringify(obj[i]);
}

The output generated is:

myObject0: {"type":"name", "id":"123"}
myObject1: {"type":"name2", "id":"456"}
myObject2: {"type":"name3", "id":"789"}


回答3:

If you're talking about parsing it uses Zapier's Code By Zapier JavaScript engine, then here's what you do:

Zapier returns whatever object you tell it to, so assuming you use their standard name for input, here's what you do:

output = {};

for (var i = 0; i < input["data"].length; i++) {
  output["myObject"+str(i)] = input.data[i];
}

This should return an object called output that looks as follows:

"output" : {
    "myObject0" : {  
      "type": "name",
      "id": "123"
    },
    "myObject1" : {
      "type" : "name",
      "id": "124"
    }
}