Best answer

I get an array of objects from an api, but the data is not right

  • 8 November 2021
  • 4 replies
  • 1138 views

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.

 

 

icon

Best answer by Saad Ahmad 9 November 2021, 14:52

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 replies

Userlevel 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 data[i] === "undefined") data[i] = {};

    data[i][key] = li[i];

    // add a record number (in case we want to break the fork/loop with a Filter)

    data[i].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:

  1. start an action
  2. choose formatter by zapier
  3. choose utilities in `choose an event`
  4. in transform field, choose Line Item to Text
  5. in input field, choose the line item field you want to convert to separate text items
  6. confirm and complete test, that should separate the values for you.
  7. the text values should show from that step of the zap, so make sure to rename your steps to something relevant to the data.
  8. e.g.
    items from an array of objects showing separately.

     

Userlevel 7
Badge +11

Thanks for sharing how you solved this, @Saad Ahmad. I’m so glad to see that you were able to get it working! :)