Question

Custom request generates broken json

  • 23 April 2023
  • 6 replies
  • 83 views

I’m trying to perform a GET request to third-party API using “Custom Request in Webhooks by Zapier”

Result looks fine:

However when I passing the returned data to the “Run Javascript in Code by Zapier” it is not passed as JSON, but as some text string:

 

 

 

How can I get and iterate JSON?

What is this format?

I wanted to iterate JSON to pick specific fields for every item in array and format them into one string. Maybe it should be done differently?

 


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

6 replies

Userlevel 6
Badge +8

Use JSON.parse(inputData)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Userlevel 6
Badge +8

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:

 

 

 

Userlevel 6
Badge +8

@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][j].product_type) {
newResults += results[i][j].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);

let results = [
[
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
],
[
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
],
[
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips",
},
]
];

 

@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

Userlevel 6
Badge +8

@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.

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 [object Object] items you see in the stringified version).

So the data it is sending (without Zapier’s UI adjustment) looks like:

{             // Start of outer JSON object
results: [ // Start of first outer array
[ // Start of first inner array
{ // Start of first inner JSON object
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips"
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips"
}
],
[{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips"
},
{
pk: "3085032133219631437",
id: "3085032133219631437_9022316825",
code: "CrQPGFFOMFN",
taken_at: d,
taken_at_ts: "1681984530",
media_type: "2",
product_type: "clips"
}
]
]
}