# Regular expression over different sets of API response Verification

**URL:** <https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596>\
**Category:** Help Hub\
**Created:** [December 21, 2020, 7:19am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596 "2020-12-21T07:19:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [December 21, 2020, 7:19am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/1 "2020-12-21T07:19:00Z")

</div>

Hi Team,

I just want to verify my API response with one line of code(script) only.

As of now my API response is inconsistent, and response gonna be either  
“”  
OR  
“{}”

What gonna be best way to verify those responses with one line code only?

As of now my script is like:  
pm.response.to.have.body("");  
pm.response.to.have.body("{}");

I was going through chai assertion “anyof” but got to know it will work with numbers only.

Note: If response looks like below then  
{“recentOrders”:};  
who one line of code will be?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [December 21, 2020, 12:43pm UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/2 "2020-12-21T12:43:15Z")

</div>

Why do you want only one line of code? That is an odd requirement.

What I find to be the most accurate is to load the response body from your API definition document and use ajv to validate the schema. That takes more than one line, though.

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [December 21, 2020, 1:04pm UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/3 "2020-12-21T13:04:03Z")

</div>

Reason behind is, I am not sure what response gonna be. Few times its says as “” and few times as “{}”.  
So I want to make a script so that in both of the cases will work, No failure.

So looking for regular expression where something we can do.

---

<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:** [December 25, 2020, 1:31am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/4 "2020-12-25T01:31:28Z")

</div>

```auto
pm.expect(pm.response.json()).to.satisfy(function (status) {
 if (_.isEqual(status,{}) || _.isEqual(status,[])) {
           return true;
       } else {
           return false;
       }
    },"Expected response to be [] or {} but got "+pm.response.json());

```

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [December 29, 2020, 8:44am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/5 "2020-12-29T08:44:28Z")

</div>

May I know how exactly work this?  
Anyway tried this way and worked fine.  
pm.test(“data verification”, function () {

let jsonData = pm.response.json();

pm.expect(jsonData).to.eql(regular(jsonData));

});

function regular(value) {

if (value === “{}”) {

value = {};

} else {

value= ;

}

return value;

}

---

<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:** [December 30, 2020, 6:36am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/6 "2020-12-30T06:36:44Z")

</div>

you can use as it is what is the error you are getting,

is the response ? or is its {“recentOrders”:}

---

<div class="post-metadata">

**Author:** ![aakashjain8693](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@aakashjain8693](https://community.postman.com/u/aakashjain8693)\
**Post date:** [December 30, 2020, 6:57am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/7 "2020-12-30T06:57:59Z")

</div>

If any additional result, will add if else conditions there.

May I know how exactly we can use above mentioned method?

---

<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:** [December 31, 2020, 1:55am UTC](https://community.postman.com/t/regular-expression-over-different-sets-of-api-response-verification/18596/8 "2020-12-31T01:55:17Z")

</div>

its the same approach you used , satisfy checks if the result satisfies the function . Instead of using equals you can use the function you created in satisfy also
