# Test to see if value exists

**URL:** <https://community.postman.com/t/test-to-see-if-value-exists/26737>\
**Category:** Help Hub\
**Created:** [August 13, 2021, 8:55pm UTC](https://community.postman.com/t/test-to-see-if-value-exists/26737 "2021-08-13T20:55:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![codythesailpointpost](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@codythesailpointpost](https://community.postman.com/u/codythesailpointpost)\
**Post date:** [August 13, 2021, 8:55pm UTC](https://community.postman.com/t/test-to-see-if-value-exists/26737/1 "2021-08-13T20:55:03Z")

</div>

Hello,

I am still new to postman and JSON and get confused easily what is an array/object and how to correctly work with them.

I have a test script _which works_

let jsonData = pm.response.json()  
pm.environment.set(“UserVar”, pm.response.json()[0].member.criteria.children[3].key.sourceId);

This works and pulls a value like this  
2c9180897a5eb28b017a

but what I want to achieve - I want to create a Test which tell me if that value is located anywhere in any of the response: 2c9180897a5eb28b017a

I tried many things… but the one I feel is closest, I tried the following but it _does NOT work_ it gives me an “AssertionError: Target cannot be null or undefined.”

pm.test(‘Test to find value’, () =\> {  
pm.expect(jsonData.key).have.property(‘sourceId’, ‘2c9180897a5eb28b017a’);  
});

---

<div class="post-metadata">

**Author:** ![cmnazario](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/cmnazario/32/10207_2.png) [@cmnazario](https://community.postman.com/u/cmnazario)\
**Post date:** [August 13, 2021, 9:36pm UTC](https://community.postman.com/t/test-to-see-if-value-exists/26737/2 "2021-08-13T21:36:30Z")

</div>

Hi, I think this topic can help you with what you need, but if you can’t let me know I’ll help 😉

> [@How to check if exists property in json?](https://community.postman.com/t/how-to-check-if-exists-property-in-json/6602/3):
>
> Hi @pablovok, just wanted to weigh in here about using [lodash which is inbuilt in Postman](https://learning.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference/) and how you can easily get around this code piece. Using the [\_.get](https://lodash.com/docs/4.17.14#get) method from lodash, which allows safe extraction you’ll be able to avoid try...catch blocks. pm.test('Validate date', function () { var idDate = \_.get(jsonData, 'date[0].id'); if (idDate) { pm.expect(jsonData.date[0]).to.have.property('id'); } else { pm.expect(jsonData.date).to.have.property('id'); idDate = \_.get(jsonDat…

---

<div class="post-metadata">

**Author:** ![codythesailpointpost](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@codythesailpointpost](https://community.postman.com/u/codythesailpointpost)\
**Post date:** [August 17, 2021, 5:53pm UTC](https://community.postman.com/t/test-to-see-if-value-exists/26737/3 "2021-08-17T17:53:46Z")

</div>

I couldn’t get that to work @cmnazario  
But thank you for the help.

I did find a solution, so I’m going to post it, to hopefully help someone else in the future.  
Rather than searching through objects and arrays and confusing myself what-is-what.

I put this in the Tests tab.

string = JSON.stringify(pm.response.json());  
if(pm.expect(string).to.include(‘2c9180897a5eb28b017a’)){  
pm.test(“success”)  
}  
else{  
pm.test(“fail”)  
}

It will output in the console “success” if it finds the string  
It outputs a big red error in the fail scenario  
“error in evaluating the test script: AssertionError: expected”

But honestly… I spent enough time on this for now… so this works fine for what I was trying to achieve.
