# Filter json data and save id after filtering

**URL:** <https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598>\
**Category:** Help Hub\
**Tags:** tests, variables, scripts\
**Created:** [February 5, 2023, 3:44pm UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598 "2023-02-05T15:44:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![salma224](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@salma224](https://community.postman.com/u/salma224)\
**Post date:** [February 5, 2023, 3:44pm UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/1 "2023-02-05T15:44:34Z")

</div>

I wanna create list of ids after filtration data

 ![dbjson](https://us1.discourse-cdn.com/getpostman/original/3X/b/4/b425a2af657be2df0072670bf6e34f4b33271922.png)  
this code made filtration for first two objects in json file

```auto
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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 6, 2023, 10:49am UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/2 "2023-02-06T10:49:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![salma224](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@salma224](https://community.postman.com/u/salma224)\
**Post date:** [February 6, 2023, 11:16am UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/3 "2023-02-06T11:16:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 6, 2023, 11:51am UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/4 "2023-02-06T11:51:21Z")

</div>

What does your code look like?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 6, 2023, 12:22pm UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/5 "2023-02-06T12:22:08Z")

</div>

Is this what you want?

```auto
// 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);

```

---

<div class="post-metadata">

**Author:** ![salma224](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@salma224](https://community.postman.com/u/salma224)\
**Post date:** [February 6, 2023, 1:00pm UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/6 "2023-02-06T13:00:43Z")

</div>

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

```auto
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);
  }
});
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [February 6, 2023, 1:47pm UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/7 "2023-02-06T13:47:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 7, 2023, 1:15pm UTC](https://community.postman.com/t/filter-json-data-and-save-id-after-filtering/43598/8 "2023-10-07T13:15:02Z")

</div>

Closed on October 7, 2023, 1:15pm UTC

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