Best answer

Creating an array from a get webhook response nested json.

  • 17 October 2020
  • 15 replies
  • 2793 views

Userlevel 1

 

 

 

Hi,

 

The current Zapier steps i have set up creates a GET request to an external service. that service replies with a list of data that is nested. My end step i need to do is make multiple PUT requests to another API with part of the URL being a value from the response from the GET. There is not a fixed number of id’s/times that it will need to PUT. 

Currently if i do it with just the GET then the next step is the PUT it puts all of the values of the ID i need to put at the end of the API url as just a comma separated list. I need them to make separate PUT requests for Each ID.

Any help would be greatly appreciated.

This shows the response to the GET request (Images shows only the first part. There will are more in the response)
 

This is the PUT request. It currently puts them as a comma separated list. which causes an error. each of the values needs to process as a separate PUT.
icon

Best answer by ikbelkirasan 9 December 2020, 03:56

View original

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

15 replies

Userlevel 7
Badge +8

Hi @arusso !

Tagging in @ikbelkirasan and @olivialook  to see if they can help you crack this one! Thank you in advance :relaxed:

We need help with this problem too!  Any solutions?

@arusso @Liz_Roberts @ikbelkirasan @olivialook 

Userlevel 2
Badge +1

Hi @arusso! Could using the Code by Zapier → Run JavaScript to loop through and send individual PUT requests work here? Not sure if AJAX works in Zapier but that could work

Userlevel 7
Badge +12

@info@aqualabaquaria.com - You can solve this problem by adding a code step to split the comma-separated values. The script should return an array of objects, one object per card_number value.

Since the script is returning an array, each item will trigger the next steps of the zap multiple times, once per item.

const { cardNumbers } = inputData;
if (!cardNumbers) {
return [];
}

return cardNumbers.split(",").map((cardNumber, key) => {
return {
id: key,
card_number: cardNumber,
};
});

 

@ikbelkirasan thanks for the response.  I’m still having trouble.  Attached is my screengrab.  I tried to change the names in your code to match mine but not sure if I did it right.  Obviously not, because it’s still not working.

 

 

Userlevel 7
Badge +12

@info@aqualabaquaria.com - Can you please show me a screenshot of the output of the step 5 of your zap? Thanks!

here you go @ikbelkirasan 

 

 

Userlevel 7
Badge +12

Hi @info@aqualabaquaria.com 

Please try the following code snippet and show me a screenshot of the result? Thanks!

const { userAPIKey } = inputData;

let results = [];

if (userAPIKey) {
results = userAPIKey.split(",").map((userAPIKey, i) => {
return {
id: i + 1,
userAPIKey,
};
});
}

console.log(results);

return results;

 

Looks like it’s still not quite what we want, @ikbelkirasan 


To be perfectly honest, the most ideal situation would be to help me pair the user ID associated with the API Key so the APIKey can be inserted as a variable in a URL for an API call.  Any idea on easiest way to accomplish that?  As you can see in my step 5 results, each employee has an ID and an API KEY, and I’m trying to get a single API Key for a specific employee to use in the custom URL

 

Thanks for your help so far!

Userlevel 7
Badge +12

@info@aqualabaquaria.com - In that case, try the following code. Make sure to pass the API keys as userAPIKeys and the user IDs as usersIds.

let { userAPIKeys, userIds } = inputData;

let results = [];

userAPIKeys = userAPIKeys ? userAPIKeys.split(",") : [];
userIds = userIds ? userIds.split(",") : [];
if (userAPIKeys.length !== userIds.length) {
throw new Error("API keys number doesn't match the user IDs number");
}

results = userIds.map((id, i) => {
return {
id,
userAPIKey: userAPIKeys[i],
};
});

console.log(results);

return results;

 

@ikbelkirasan Here is what I got.  Please let me know if I set it up right.

 

 

Userlevel 7
Badge +12

@info@aqualabaquaria.com Yes, that’s right. Now you should map the id and userAPIKey in the next step and it should work fine.

@ikbelkirasan I still seem to be having some trouble, take a look at the screen grab to see the avaoiable variables from the javascript output.

 

 

How do I pull the API Key I want out of the Runtime Meta Logs field?  I already have the employee ID as a variable from a previous step, and I want to use that as an input to then spit out the APIKey for that employee so I can put that into the authorization of the subsequent webhook operation.

Userlevel 7
Badge +12

@info@aqualabaquaria.com  Here’s what you need to map to those fields. You might think that it’s just a single user ID and a single user API key. In fact, those are just samples. When the zap goes live, this step will run multiple times, once per user ID and API key.