Search code examples
javascriptarraysjavascript-objectszapier

Zapier breaks down arrays of objects into distinct arrays containing the keys of the objects


Hi everyone I am having an issue with code by zapier (javascript)... (I know, I know)

The issue I am having is I am bringing in my data from airtable. The data comes in as three distinct arrays. They are:

specCategory[]
specName[]
specDescription[]

I iterate over the arrays and split them out by the commas in the arrays. I then form each of these values in each of the arrays into their own object. I then push that object into an array.

My end goal is to push a JSON payload into PDFMonkey in the form:

{
    "payload": [
        {
            specName: "specName data",
            specCategory: "specCategory data",
            specDescription: "specDescription data"
        },
        {
            specName: "specName data",
            specCategory: "specCategory data",
            specDescription: "specDescription data"
        },
        {
            specName: "specName data",
            specCategory: "specCategory data",
            specDescription: "specDescription data"
        }
    ]
}

Zapier seems to return the correct payload for me. That is, an array of objects. However, when I go to access the data in a subsequent step, the data is returned back into three distinct arrays again.

Here is what the output from the zapier code looks like.

specArray
1
specCategory
Kitchen Appliances
specName
Gas Hob
specDescription
Westinghouse WHG 643 SA Gas Hob
2
specCategory
Kitchen Appliances
specName
Range Hood
specDescription
Westinghouse WRR 614 SA Range Hood
3
specCategory
Kitchen Appliances
specName
Oven
specDescription
Westinghouse WVE 613 S Oven
4
specCategory
Doors and Windows (Internal)
specName
Architraves
specDescription
42X12min Splayed Profile F/Joint Pine painted gloss
5
specCategory
External Stairs
specName
External Stair Balustrade
specDescription
Painted pre-primed ladies waist handrail with slats and bottom rails (not included if stair is under 1m in height)

Instead when accessing it in subsequent steps I receive three distinct arrays like:

specArraySpecName: specName[1],specName[2],specName[...],
specArraySpecCategory: specCategory[1],specCategory[2],specCategory[...],
specArraySpecDescription: specDescription[1],specDescription[2],specDescription[...]

Here is my code so you can have a look and see if what I am doing is wrong. When I try to output just the array of objects (instead of first wrapping the array in object tags) it outputs the single value of each object but the problem is that makes zapier loop the subsequent steps each time using each object as an input.

Is there a way to flatten or stringify the JSON object I am trying to create?

My code below for reference:

// this is wrapped in an async function
// you can use await throughout the function
    
let categories = inputData.specCategories.split(/\s*,\s*/);
let names = inputData.specName.split(/\s*,\s*/);
let descriptions = inputData.specDescriptions.split(/\s*,\s*/);
    
let specArray = [];
    
// for loop to input each of the discrete items into an array
for (let i = 0; i < categories.length; i++) {
    let spec = {
        specCategory: categories[i], 
        specName: names[i], 
        specDescription: descriptions[i]
        specArray.push(spec); 
    }   
    output = { specArray };

I apologise in advance for the formatting but stack overflow would not let me post code blocks due to some not properly formatted code (tried ctrl + k, 4 space, triple back ticks etc) and I could not figure it out.

Thanks for your help!


Solution

  • Great question! It's worth mentioning that this is Zapier "working as expected"... for use cases that don't match yours. This behavior supports line items, a common structure in Accounting & E-Commerce. But, that doesn't help you, but you want Zapier to stop messing with your nicely structured values.

    The best way to handle this is probably to stringify the whole JSON. Zapier only mangles arrays, so it'll leave a JSON string unharmed.

    That would be something like this in the code:

    // ...
    output = { result: JSON.stringify(specArray) };
    

    With any luck, you'll be able to use that payload in a body field for PDFMonkey and it'll process correctly!

    But, that's how I share JSON between Zapier steps to keep it un-mangled.