Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 || [];
}
like image 697
Wesley Davis Avatar asked Apr 03 '16 02:04

Wesley Davis


People also ask

Can you JSON parse an array?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

What is JSONPath parse?

JSONPath is an expression language to parse JSON data. It's very similar to the XPath expression language to parse XML data. The idea is to parse the JSON data and get the value you want.


2 Answers

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,

like image 39
Mike Crews Avatar answered Oct 28 '22 15:10

Mike Crews


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"}
like image 158
Brian Schuster Avatar answered Oct 28 '22 13:10

Brian Schuster