Let’s explore how to restructure a JSON array of objects in Zapier to access object key data. More specifically, how to reduce a JSON array to make the values for each object key available in subsequent zap steps.

Notably, this is designed to “solve” the issue of Zapier grouping object key values into a comma-separated list (frustrating in many cases). With this method, we can access each individual object key’s value as a variable.

In other words, if you are tired of seeing something like this:

…and you want to see something more like this:

…then read on.

How to restructure a JSON array of objects in Zapier

In many cases, you will receive a JSON array of objects after making a webhook GET request in Zapier. You may also receive a JSON array of objects back after doing the same thing via their Code module with fetch.

My preferred method is to use fetch via the Zapier code module (Node). First, we request the raw HTML response body as follows:

In my case, this yielded a JSON array of objects with the following structure:

{“Success”:true,”Error”:null,”TotalPages”:1,”TotalRecords”:26,”CurrentPage”:1,”PayLoad”:[{“ItemName”:”Product A”,”QuantityOnHand”:147},{“ItemName”:”Product B”,”QuantityOnHand”:155}]}

At this point, I wanted to access the object key values in subsequent zap steps as shown here:

  • Product A QuantityOnHand – 147
  • Product B QuantityOnHand – 155

To accomplish this, we must process the raw JSON response in another Code step as follows:

Note that I passed the raw JSON response into a variable named “response” in this second step:

This is where the inputData.response variable comes from in the above code. Likewise, the PayLoad variable in the above code is the name of my specific array of objects. You will need to change this to match the name of your array, of course. Finally, you will note that ItemName and QuantityOnHand are the names of the object keys I am targeting.

In the end, this creates a new object with the individual key values from each object nested under each ItemName. And it’s this structure that allows the individual key values to be accessible in subsequent zap steps for each ItemName, without running into the issue of a comma-separated list or looped zap.

Thanks also to Chris Burgin over at Daily JS for this helpful article, which is largely where the second part of this code came from: https://medium.com/dailyjs/rewriting-javascript-converting-an-array-of-objects-to-an-object-ec579cafbfc7

Share This Article:

Liked this article? Sign up for more.

Steve believes your greatest creation happens when you combine art and science. Join his 10K+ followers on social media and email today.

100% Privacy. No spam. Unsubscribe anytime.

Related Posts

Leave a Reply