# Search for a specific value in JSON

**URL:** <https://community.postman.com/t/search-for-a-specific-value-in-json/39205>\
**Category:** Help Hub\
**Tags:** tests, variables, json\
**Created:** [September 18, 2022, 11:16am UTC](https://community.postman.com/t/search-for-a-specific-value-in-json/39205 "2022-09-18T11:16:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![supply-meteorologis4](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@supply-meteorologis4](https://community.postman.com/u/supply-meteorologis4)\
**Post date:** [September 18, 2022, 11:16am UTC](https://community.postman.com/t/search-for-a-specific-value-in-json/39205/1 "2022-09-18T11:16:03Z")

</div>

_My question_:  
I’m trying to check response body if there’s a user which has last\_name = “Holt” in following [JSON file](https://reqres.in/api/users?page=1). with writing a test script in Postman

_I’ve already tried_:

```auto
pm.test("Last Name Holt", function () {
    var jsonData = JSON.parse(responseBody);
    value = pm.expect(jsonData[0].last_name).to.eql("Holt");
});

```

and

```auto
pm.test("Last Name Holt", function () {
    const responseJson = pm.response.json();
    pm.expect(responseJson.data[3].last_name).to.eql("Holt");
});

```

2nd code piece returns true but that’s directly looking into data[3] I want to search amongst all last\_names in the file.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 18, 2022, 5:56pm UTC](https://community.postman.com/t/search-for-a-specific-value-in-json/39205/2 "2022-09-18T17:56:26Z")

</div>

Hi @supply-meteorologis4

Try using a for loop… something like this;

```auto
const jsonData = pm.response.json();

pm.test("Check last Name Holt exists.", function () {
    for (let i = 0; i < jsonData.data.length; i++) {
        if(jsonData.data[i].last_name === "Holt"){
            console.log("The last name Holt exists.")
        }else{
            console.log("The last name Holt not found.")
        };
    }
});

```

---

<div class="post-metadata">

**Author:** ![supply-meteorologis4](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@supply-meteorologis4](https://community.postman.com/u/supply-meteorologis4)\
**Post date:** [September 19, 2022, 11:18am UTC](https://community.postman.com/t/search-for-a-specific-value-in-json/39205/3 "2022-09-19T11:18:27Z")

</div>

Hey @w4dd325

I tried this one:

```auto
pm.test("Last Name Holt", function () {
  const responseJson = pm.response.json();
  pm.expect(responseJson.data.some(o => o.last_name == 'Holt')).to.be.true;
});

```

But yours as well looks like a thing to get the job done. Thank you!

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 19, 2022, 11:40am UTC](https://community.postman.com/t/search-for-a-specific-value-in-json/39205/4 "2022-09-19T11:40:24Z")

</div>

You could also use “find”.

```auto
const jsonData = pm.response.json();

pm.test("Check last Name Holt exists.", () => {
    let result = jsonData.data.find(a => a.last_name === "Holt")
    console.log(result);
    pm.expect(result.last_name).to.eql("Holt")
});

```

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [September 19, 2022, 11:47am UTC](https://community.postman.com/t/search-for-a-specific-value-in-json/39205/5 "2022-09-19T11:47:21Z")

</div>

Also, if it helps…

If you were looking for multiple matches, you could use “filter”… It finds all the matches and stores them in a new array, so you would have to reference the array index you wanted to assert;

```auto
const jsonData = pm.response.json();
pm.test("Check last Name Holt exists.", () => {
    let result = jsonData.data.filter(a => a.last_name === "Holt")
    console.log("Number of matches = " + result.length)
    console.log(result);
    
    //(note the [0] for array index)
    pm.expect(result[0].last_name).to.eql("Holt")

});

```
