I am currently working through a project where I am given a long list of profiles. I am specifically searching for profiles that have the tag “archived:true”. I have my API call and can see the flag. I have saved my results to an environmental variable. But I am unsure/confused how I can then use the variable to parse for only the IDs that are “archived:true”. Any thoughts would be appreciated
Can you please show an example response?
You can trim it to three records if the response is large.
The way I would do this is filter on the response first before saving it to an environment variable.
Postman uses JavaScript under the hood, so you can use the JavaScript filter function or you can also use the Lodash library which is available in the sandbox.
Hey @zperk11, welcome!
Since I don’t know the exact shape of the response, I’ll do a best guess for the code you’re looking for. In a post-response script in your request that returns the profile list, you can do something like this, assuming there’s a property called profiles
in the response.
const data = pm.response.json();
// Find the archived profiles
const archivedProfiles = data.profiles.filter(p => p.archived === true);
// Save the archived profiles to an env variable
pm.environment.set('archivedProfiles', JSON.stringify(archivedProfiles));
Then in a subsequent request, you can get your list of profiles by accessing that variable and parsing the data from it, likely in a pre-request script.
const archivedProfiles = JSON.parse(pm.environment.get('archivedProfiles'));
// Get the first one, if you want
const profile = archivedProfiles.pop();
// Save the profile list without the profile you just popped
pm.environment.set('archivedProfiles', JSON.stringify(archivedProfiles));
Hopefully that helps! If you’re trying to do something else, please let me know.
Slightly off topic, but doesn’t pop() return the last element of the array.
shift() returns the first element (which I use a lot to control sendRequest loops).
Hi Allen thanks for the response.
After doing my get call I get a response like
ID
UID
NAME
PROFILE TYPE
archived: true or obviously could be false
Each time I try to filter using let archivedids=total users.filter(id =>id.archived === true);
I get that totalusers.filter is not a function. At this point I just want to return IDs that are archived = true. Then I can worry about the rest later
Yup, you’re right.
Order probably doesn’t matter in this case since he’s likely processing all of the profiles, but yes - pop
removes the last item in the array.
Where does totalusers
come from? It’s giving you that error because that variable is not an array.
Would you be able to send a screenshot of your request in Postman or copy/paste the response? From your last message, it looks like the GET
call is only returning a single profile? If that’s the case, then you’d modify to something like this because you’re likely iterating over the GET
for every profile.
let totalUsers = pm.environment.get('totalUsers');
if(!totalUsers) {
totalUsers = [];
} else {
totalUsers = JSON.parse(totalUsers);
}
const data = pm.response.json();
if(data.archived) {
totalUsers.push(data.id);
}
pm.environment.set('totalUsers', JSON.stringify(totalUsers));
This is why we ask for example responses.
We need to see the JSON response to accurately provide example code.
Otherwise its a lot of guess work and assumptions and invariably won’t work.