6

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 || [];
}
Wesley Davis
  • 87
  • 1
  • 1
  • 4
  • I'm not familiar with Zapier, are you essentially asking for a function that starts with your json and returns that object? – omarjmh Apr 03 '16 at 03:00

3 Answers3

6

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"}
Orlandster
  • 4,706
  • 2
  • 30
  • 45
4

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,

Mike Crews
  • 56
  • 3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11879934) – Aziz Apr 04 '16 at 02:05
  • 1
    Edited original post to do just that. Thanks for the constructive advice. – Mike Crews Apr 04 '16 at 08:16
1

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"
    }
}
George Stocker
  • 57,289
  • 29
  • 176
  • 237