Hey everyone, I’m having a weird problem with Zapier and JSON arrays. When I use a code action, Zapier is splitting my arrays into two separate variables. It’s super weird!
Here’s what’s happening:
- If I put the array directly in the code action, everything works fine.
- But if I try to get the array from another step, it gets split up.
I’ve tried this code:
let parsedData = JSON.parse(input.jsonString);
let originList = input.sources;
let originArr = originList.split(',');
let results = [];
parsedData.items.forEach(item => {
originArr.forEach(origin => {
if (item.category === origin) {
results.push(item.uniqueCode);
}
});
});
return { resultId: 456, data: results.join(',') };
Anyone know why this is happening or how to fix it? I’m totally stuck!
I’ve dealt with similar Zapier quirks before. One thing that often helps is explicitly typing your variables. Try this:
let originArr = (typeof input.sources === ‘string’) ? input.sources.split(‘,’) : input.sources;
This forces Zapier to treat the input correctly whether it’s a string or already an array. Also, consider using input.sources?.split(‘,’) || to handle cases where input.sources might be undefined.
Another tip: Zapier’s ‘Code’ step can be finicky with complex objects. If possible, try to simplify your data structure before it reaches the Code step. You might need to adjust your workflow to pre-process the data in earlier steps.
Lastly, always log your variables (console.log()) at each stage. It helps pinpoint exactly where things go wrong. Hope this helps!
I’ve encountered similar issues with Zapier’s code actions. One potential solution is to use the Array.isArray() method to check if input.sources is already an array. If it is, you can skip the split operation. Here’s a modified version of your code that might help:
let parsedData = JSON.parse(input.jsonString);
let originArr = Array.isArray(input.sources) ? input.sources : input.sources.split(',');
let results = parsedData.items.filter(item => originArr.includes(item.category)).map(item => item.uniqueCode);
return { resultId: 456, data: results.join(',') };
This approach simplifies the logic and should be more resilient to different input types. Also, ensure that the data you’re passing from other steps is properly formatted before it reaches the code action.
hey ethan, sounds like a tricky one! have u tried using JSON.stringify() on ur array before passing it to the code action? sometimes zapier gets weird with complex data types. also, double-check that ur input.sources is actually a string - might already be an array. good luck mate!