Filter json data and save id after filtering

I wanna create list of ids after filtration data


this code made filtration for first two objects in json file

const response = pm.response.json()//
let allTracks = response.map(item => item.track);
//console.log(allTracks);

if( allTracks.filter(track => track != "Software Testing"))
{
 let  IDtrackss = response.map(item => item.id); 
  console.log(IDtrackss);
}

i wanna filter all data in json file and save ids after filtration

You need to reverse the logic.

Do the filter first against the response object.

Then run the map on the filtered object to return the elements you want.

You should not need an IF statement.

when I try this give me empty array of items after filtration

What does your code look like?

1 Like

Is this what you want?

// parse to contantInfo array
const response = pm.response.json().contantInfo;

// filter first
let tracks = response.filter(obj => obj.track != "Software Testing");
console.log(tracks);

// then return ID's
let IDs = tracks.map(item => item.id);
console.log(IDs);

// This can be done in one line
let IDsV2 = response.filter(obj => obj.track != "Software Testing").map(item => item.id);
console.log(IDsV2);

thank you it’s work
can i ask you another question
i wanna make patch request after filtration ,console don’t give me error but it’s not modified
i write this code in another request in pre_rquest

const IDsV2=pm.environment.get("IDsV2");
console.log(IDsV2);
for (let i = 0; i < IDsV2.length; i++) {
  let value=IDsV2[i];
  const PatchRequest = {
  url: 'http://localhost:3000/contantInfo/'+value,
  method: 'PATCH',
  body: {
    "certifications": "not requried"
  }
};
pm.sendRequest(PatchRequest, (error, response) => {
  if (error) {
    console.log(error);
  }
});
}

Check the console log for the patch requests.

Does the URL look correct? Is the Id being added to the end as expected.

If it is, then it will be something with how the API is handling the patch request. (Which I can’t answer for you as I don’t know anything about the API and what its expecting).

To test the patch request on its own, create a single patch request with the values hard coded and see if that works.

When you console log IDsV2 on the second line, is it actually returning an array?

You usually have to stringify() arrays when storing them as a collection or environment variables, and JSON.parse() them when retrieving or they will just be treated as a string.

On a side note, why can’t this just be added to the end of the code in the Tests tab so you don’t have to save it to an environment variable at all. Is it being used in another request?

Finally, although you can use a for loop. As the data is an array you can also use a forEach loop instead which is slightly easier to handle. You also don’t need to declare the value like you are doing. You can just replace “value” with “i” as the iteration data is already available in that variable.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.