I get an array of objects from an api, but the data is not right
I’m using the Zapier Platform CLI to develop a zapier app integration. I have rest hook that gets triggered when a document in my database changes, and returns that document. It contains objects, and array of objects. Instead of getting it in some sort of an array format, I get all the ones of the same key in the same option field. for example, Clayton and Perry are 2 first names from 2 separate objects from an array.
How can I make it so I get them separately, and hopefully some sort of identification of which item belongs to which index of the array?
I’m very new to zapier still, any help would be much appreciated.
Â
Â
Page 1 / 1
I’m unsure of where this is posted elsewhere up here but I use the following Javascript Code step immediately following the step you’ve described. The result here is that the code step will emit an object for each (very much like a ‘for...next’ loop). It’s very handy but remember that each object will trigger ALL steps following this one.
Â
/* Add as many fields as you need under "Input Data", each with a unique name. The associated source of the data should be a comma separated text or mapped line items field. The code here will find each Input Data field and generate an output of an array of objects with the same structure.Â
Arrays containing objects will cause all following ZAP steps to be executed for each element the array. Very much like a for...next loop
// get Input Data field names by grabbing the 'keys'
let keys = Object.keys(inputData)
let data = /];
// Add any number of 'inputData' fields above. This will assemble the objects for you
// loop through each Input Data field
for (let key of keys) {
 // split the contents of each Input Data field on the commas into an array
 let li = inputData key].split(",");
 for (let i=0; i<li.length; i++) {
  if (typeof datapi] === "undefined") data]i] = {};
  dataii]=key] = lipi];
  // add a record number (in case we want to break the fork/loop with a Filter)
  datati].recordNumber = i+1;
 }
}
// preview the whole data structure in the output
// console.log(data);
// output the data as an array contain 'keys' number of objects
output = data;
Thanks for the reply. But that just returns to me a single item from the field that I input, where I want all the items coming in separately.
Okay so I found out a way, which was probably the way it was meant to be, but after I get my data back which is an object, which contains an array of objects called previousNames, it gets turned into something called line items. After that, you:
start an action
choose formatter by zapier
choose utilities in `choose an event`
in transform field, choose Line Item to Text
in input field, choose the line item field you want to convert to separate text items
confirm and complete test, that should separate the values for you.
the text values should show from that step of the zap, so make sure to rename your steps to something relevant to the data.
e.g.
Â
Thanks for sharing how you solved this, @Saad Ahmad. I’m so glad to see that you were able to get it working! :) Â