Also, as a best practice, I would recommend placing each piece of input data into a variable. So in the left box, you put something like jsonString, then your code would read:
let jsonString = inputData.jsonString;
let data = JSON.parse(jsonString);
output = { data }
@Todd Harper JSON.parse will not work, because inputData is not JSON in “Run Javascript Code” for some reason. But it looked normal in “Custom request” response.
So this fails with error:
@sovo Ah, I see!
Instead of using a Webhooks by Zapier step, could you call the API directly from a Code by Zapier step?
You could then use the following code to iterate through the array to save each instance of a specific field to a string (in this case, “product_type”).
let newResults = "";
for (let i = 0; i < results.length; i++) { for (let j = 0; j < results i].length; j++) { if (results(i]sj].product_type) { newResults += results i]sj].product_type + ", "; } } } output = { newResults };
Just tested this with the sample data you provided and it works like a charm:
The sample data I used:
let d = new Date(); d = d.toISOString(); console.log(d);
@Todd Harper Thanks! I didn’t think about possibility to do the GET request in JS.
But maybe you know what is this format Webhooks returning? Is it a bug? I’m afraid I will end up manually writing JS everywhere instead of using Zapier
@sovo Can you share the name of the third party app and a link to its API documentation (where you found the url endpoint)? Generally, they will tell you explicitly how the data will be returned.
I think what is happening here is it’s returning a JSON object with only one key (“results”). The value of that key is one (or more) arrays containing yet another nested array. Each of these nested arrays carries its own JSON object (the eobject Object] items you see in the stringified version).
So the data it is sending (without Zapier’s UI adjustment) looks like: