# Compare responses from two POST requests

**URL:** <https://community.postman.com/t/compare-responses-from-two-post-requests/19206>\
**Category:** Help Hub\
**Tags:** compare-two-response\
**Created:** [January 15, 2021, 1:07pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206 "2021-01-15T13:07:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![prabal19](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@prabal19](https://community.postman.com/u/prabal19)\
**Post date:** [January 15, 2021, 1:07pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/1 "2021-01-15T13:07:25Z")

</div>

Let us say, there are two APIs which accept POST requests. Both the APIs perform the same job(one is an old API and another is the new API, there has been some migration of code and hence the API is also getting renewed).

As regression, I am required to send POST requests to both the APIs then compare the responses(the entire response body **except few fields like ID, Timestamp etc** ). There SHOULD NOT be any difference. If there is a difference then it is a bug.

Here is the catch:  
The comparison for the entire JSON response has to be done 100+ times with different bodies in the POST request. (I am not sure if getting the data from a csv file is a good option for 100+ data).

Looking forward to hearing from you on how you would like to proceed on this problem of comparing two JSON response bodies in POSTMAN and repeat for 100+ times EXCLUDING one or more fields like IDs, Timestamp which will never match.

---

<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:** [January 16, 2021, 6:32pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/2 "2021-01-16T18:32:56Z")

</div>

postman support lodash so its as simple as:

```
console.log(_.isEqual(oldresponse, newresponse, (value1, value2, key) => {

    return key === "key1" || key === "key2" ? true : undefined;

}))

```

were key1,key2 etc are keys you don’t want to compare.

1. so create two requests,

2. in first request (old endpoint) store value to an environment varaible say ‘oldresponse’

3. in second request ( new endpoint) store value to ‘newresponse’

4. And in test script session of second request just use

```auto
console.log(_.isEqual(pm.environment.get('oldresponse'),pm.environment.get('newresponse'), (value1, value2, key) => {
        //this will ignore comparing the value if key is timestamp or id
        return key === "timestamp" || key === "id" ? true : undefined;

    }))
```

---

<div class="post-metadata">

**Author:** ![prabal19](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@prabal19](https://community.postman.com/u/prabal19)\
**Post date:** [January 18, 2021, 10:57am UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/3 "2021-01-18T10:57:42Z")

</div>

Thank you. So basically, if there is a mismatch then it returns False, if there is no mismatch then it returns True.

Now how to find out what didn’t match?

---

<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:** [January 18, 2021, 12:02pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/4 "2021-01-18T12:02:19Z")

</div>

A way you could brute force these tests to figure out which values were different:

```auto
const ignoreProperties = ['id', 'timestamp'];
const newResponse = pm.response.json();
const oldResponse = JSON.parse(pm.environment.get('oldResponse');
const oldResponseKeys = Object.keys(oldResponse);
_.foreach(oldResponseKeys, function(key) {
  if(!ignoreProperties.includes(key)){
    pm.test(`New response has same value for `${key}` property`, function(){
      pm.expect(newResponse).to.have.property(key, oldResponse[key]);
    });
  }
});

```

This iterates over every property in the old response and makes sure the new response has the property and that the values match.

---

<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:** [January 18, 2021, 6:07pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/5 "2021-01-18T18:07:19Z")

</div>

> [@praveendvd](#):
>
> ```auto
> console.log(_.isEqual(pm.environment.get('oldresponse'),pm.environment.get('newresponse'), (value1, value2, key) => {
> //this will ignore comparing the value if key is timestamp or id
> return key === "timestamp" || key === "id" ? true : undefined;
> 
> }))
> 
> ```

```
console.log(_.isEqual(first, second, (value1, value2, key) => {

    let result= key === "mean" || key === "mean" ? true : undefined;

    result || JSON.stringify(value1) === JSON.stringify(value2) ? undefined : console.log(value1, value2, JSON.stringify(value1) === JSON.stringify(value2) );

    return result

}))

```

This will print the difference also

---

<div class="post-metadata">

**Author:** ![prabal19](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@prabal19](https://community.postman.com/u/prabal19)\
**Post date:** [January 25, 2021, 5:17am UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/6 "2021-01-25T05:17:54Z")

</div>

> [@praveendvd](#):
>
> ```auto
> console.log(_.isEqual(first, second, (value1, value2, key) => {
> 
> let result= key === "mean" || key === "mean" ? true : undefined;
> 
> result || JSON.stringify(value1) === JSON.stringify(value2) ? undefined : console.log(value1, value2, JSON.stringify(value1) === JSON.stringify(value2) );
> 
> return result
> 
> }))
> 
> ```

- {referenceNumber: “ADR-2021-100000201548”, statusCode: 2000, statusMessage: “File successfully uploaded”}

- ▶{statusCode: 2000, statusMessage: “File successfully uploaded”, referenceNumber: “ADR-2021-1000000301922”}

false

true

I have used the above script. At the end of the second POST response, it says false but at the end it says true. True is correct but not sure why it prints false as well.

---

<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:** [January 25, 2021, 10:06am UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/7 "2021-01-25T10:06:12Z")

</div>

remove the console.log inside the function , it is there to print each comparison . it will first stringify two object and validate if its equal if its not equal then it does a deep comparizon of values instead. I just added it for demo purpose

you want only the final result.

```
console.log(_.isEqual(first, second, (value1, value2, key) => {

    let result= key === "mean" || key === "mean" ? true : undefined;

    return result

}))
```

---

<div class="post-metadata">

**Author:** ![prabal19](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@prabal19](https://community.postman.com/u/prabal19)\
**Post date:** [January 26, 2021, 9:06am UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/8 "2021-01-26T09:06:46Z")

</div>

Understood, I actually want only the differences between the two. With the above I need to manually check where the difference is because it returns both same and difference.

---

<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:** [January 26, 2021, 12:20pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/9 "2021-01-26T12:20:43Z")

</div>

Did you try the script I recommended?

---

<div class="post-metadata">

**Author:** ![prabal19](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@prabal19](https://community.postman.com/u/prabal19)\
**Post date:** [January 27, 2021, 4:57am UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/10 "2021-01-27T04:57:32Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> const ignoreProperties = ['id', 'timestamp'];
> const newResponse = pm.response.json();
> const oldResponse = JSON.parse(pm.environment.get('oldResponse');
> const oldResponseKeys = Object.keys(oldResponse);
> _.foreach(oldResponseKeys, function(key) {
> if(!ignoreProperties.includes(key)){
> pm.test(`New response has same value for `${key}` property`, function(){
> pm.expect(newResponse).to.have.property(key, oldResponse[key]);
> });
> }
> });
> 
> ```

I get the below error - JSONError: Unexpected token ‘o’ at 1:2  
[object Object]

---

<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:** [January 27, 2021, 12:26pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/11 "2021-01-27T12:26:52Z")

</div>

Make sure to do `JSON.stringify` on your response before saving it to an environment variable.

```auto
pm.environment.set('oldResponse', JSON.stringify(pm.response.json()));

```

---

<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:** [January 27, 2021, 12:44pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/12 "2021-01-27T12:44:25Z")

</div>

That might not work in case of nested properties

---

<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:** [January 27, 2021, 12:58pm UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/13 "2021-01-27T12:58:17Z")

</div>

No it wouldn’t, but it’s easy enough to add a recursive function in there to navigate through all the nested objects.

I didn’t see mention of nested objects in the post, so the thought hadn’t crossed my mind.

---

<div class="post-metadata">

**Author:** ![sanjutvj](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@sanjutvj](https://community.postman.com/u/sanjutvj)\
**Post date:** [November 9, 2021, 8:56am UTC](https://community.postman.com/t/compare-responses-from-two-post-requests/19206/14 "2021-11-09T08:56:53Z")

</div>

Thank you for your post. Can you please help me to iterate nested objects for comparing postman request.

Sample JSON  
{  
“value”: [  
{  
“DocumentId”: “”,  
“DocumentClass”: “”,  
“MetaData”: [  
{  
“PropertyName”: “”,  
“Type”: “”,  
“Value”: “”  
},  
{  
“PropertyName”: “”,  
“Type”: “”  
“Value”: “”  
}  
],  
}
