# How to validate whether query parameter value in the GET req is a substring of the string present in the JSON response?

**URL:** <https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250>\
**Category:** Help Hub\
**Tags:** tests, url-params, request-params\
**Created:** [April 4, 2021, 3:08pm UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250 "2021-04-04T15:08:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saisubhashini](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@saisubhashini](https://community.postman.com/u/saisubhashini)\
**Post date:** [April 4, 2021, 3:08pm UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/1 "2021-04-04T15:08:01Z")

</div>

Hi,  
I am new to Postman. I recently started working on API testing as part of my work. I have a usecase where I need to validate whether JSON response has records filtered based on the substring passed as a query parameter in the GET request.

The following is the JSON response I have

 ![pic](https://us1.discourse-cdn.com/getpostman/original/2X/b/b464fb53a740da3a1a6357c93f99299a21e4803b.png)

The API is {{url}}/api/control-branch-master?branchCode=sa&branchName=test

I have two input parameters “branchCode” and “branchName”. Based on the value passed in the request for these two parameters, those records containing these values as substring will be filtered and returned in JSON response .

I have written the following script

 ![script](https://us1.discourse-cdn.com/getpostman/original/3X/b/1/b1b5d79b496d9fd35c7b1d299978fa154d0c88ff.png)

I have tried validating the string by using “has()”. I do not know if there is any alternate way though I tried searching alot.

The above script runs without any errors or failed cases. But irrespective of the no. of query parameters passed in the request, the console prints both the console.log statements in if and else. The request can contain single or both query parameters. I have also made some changes and in the “has” part of the above script and wrote as follows

 ![script2](https://us1.discourse-cdn.com/getpostman/original/3X/2/6/26da9b718133908d2510a5ab4e9b4cebb4b08233.png)

Even in the above script irrespective of making any changes to the has() part, the console.log statements of both if else cases get printed and not else if.  
I am actually not sure whether the above script is the right way to validate the input substring with a string in response.

I Kindly request anyone to help me out with this as am completely a beginner in this . What is the right way to validate the input with response and is the above script right? What changes can I make in above script to make it right? Is there an alternative way to do this by using regular expression or include()?

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [April 6, 2021, 6:21pm UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/2 "2021-04-06T18:21:46Z")

</div>

Hi there,

thanks for sharing all these details.

First of all, you are overcomplicating your tests, making them hard to read.

One more thing: I don’t think pm.expect does not return a boolean you can use in your if blocks.

When you are dealing with a problem you don’t know to solve, try breaking it down in smaller pieces.

For example:

1. Get the data you want to assert logged with console.log.
2. Write an assertion using hardcoded data.
3. Use Postman variables to make the assertion dynamic.

I hope this helps.

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [April 6, 2021, 11:10pm UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/3 "2021-04-06T23:10:57Z")

</div>

pm.expect throws an exception , it doesn’t return boolean so using it inside if statement doesn’t do anything.

also to assert substring use

```
       pm.expect("dasd").to.contain("hi")

```

postman uses chai:

[https://www.chaijs.com/api/bdd/](https://www.chaijs.com/api/bdd/)

---

<div class="post-metadata">

**Author:** ![saisubhashini](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@saisubhashini](https://community.postman.com/u/saisubhashini)\
**Post date:** [April 8, 2021, 8:07am UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/4 "2021-04-08T08:07:50Z")

</div>

Hi…Firstly thank you for replying. Thank u for sharing the suggestions. Will follow them hereafter. I have rewritten my script above and it worked out. Though I have seen your reply only today. But as u said will try not complicating them hereafter. I just want to share a screnshot of what I have written

[![](https://i.imgur.com/BXL5scu.png) ](https://i.imgur.com/BXL5scu.png)  
[![](https://i.imgur.com/x5LTRez.png) ](https://i.imgur.com/x5LTRez.png)

The above script however worked. Am not sure if there is any other way to write it.

---

<div class="post-metadata">

**Author:** ![saisubhashini](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@saisubhashini](https://community.postman.com/u/saisubhashini)\
**Post date:** [April 8, 2021, 8:09am UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/5 "2021-04-08T08:09:26Z")

</div>

Thanks for replying. I tried contain first, it didn’t work for me.So used has…Am a complete beginner to postman. Will take care while using pm.expect hereafter…I thought can use pm.test to check whether test passed or failed and hence put IF-else conditions within pm.expect(). However I made changes to my script and it worked before I have seen ur reply. So I used pm.expect() like earlier. This is how I wrote

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/f/dfa2781247f97586f222385c6909c6aa0cb8327b.png)

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/0/401eb54e330b243997dc27f1cc855872fe0d9e1c.png)

Should I write If-case normally without pm.expect() and just have the assertion in it?

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [April 8, 2021, 9:49am UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/7 "2021-04-08T09:49:07Z")

</div>

> [@saisubhashini](#):
>
> I have rewritten my script above and it worked out.

You can imagine how surprised I am that your code “works”. I recently did a talk around this subject. Maybe it is worth watching:

[![](https://img.youtube.com/vi/a7G8Yrb5qWU/maxresdefault.jpg "Make Sure Your Tests Fail, Valentin Despa | Postman Galaxy 2021") ](https://www.youtube.com/watch?v=a7G8Yrb5qWU)

Don’t get me wrong but I think you are missing some basics around writing assertions in Postman. It would also not hurt to know some JavaScript basics.

[![](https://img.youtube.com/vi/NEO8RqnfYec/maxresdefault.jpg "How to write assertions in Postman (objects, arrays, nested properties)") ](https://www.youtube.com/watch?v=NEO8RqnfYec)

---

<div class="post-metadata">

**Author:** ![saisubhashini](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@saisubhashini](https://community.postman.com/u/saisubhashini)\
**Post date:** [April 8, 2021, 2:10pm UTC](https://community.postman.com/t/how-to-validate-whether-query-parameter-value-in-the-get-req-is-a-substring-of-the-string-present-in-the-json-response/22250/8 "2021-04-08T14:10:23Z")

</div>

OK.Thanks for sharing
