Manipulating the response data for comparison

Hi,

I’m a real newbie and have already spent far too long reading many answers on where to try to get something that works, but I’m struggling. I want to compare two responses for a version 1 versus a version 2 endpoint (the data returned should be almost identical).

I’ve managed to crack the order of arrays with a sort before comparing, but I know that there is one attribute in the response that will differ between the two versions. This attribute is in an object within an array in two different objects in the response.

For example (snipped):

{
    "policyNumber": 1,
    "policyholder": {
        "contactDetails": {
            "address1": "line 1",
            "address2": "",
            "address3": "",
            "city": "",
            "county": "",
            "postcode": "",
            "emailAddress": "",
            "telephoneNumber": ""
        },
        "bankDetails": [
            {
                "accountName": "xxx",
                "sortCode": "999999",
                "accountNumber": "12345678",
                "mandateReference": "xxx",
                "billingDay": 1,
                "firstCollectionDate": null,
                "bankName": "",
                "effectiveFrom": "2023-06-24T00:00:00Z",
                "createdOn": "2023-06-24T23:58:41Z"
            },
            {
                "accountName": "xxx",
                "sortCode": "999998",
                "accountNumber": "12345679",
                "mandateReference": "xxx",
                "billingDay": 1,
                "firstCollectionDate": "2025-07-10T00:00:00Z",
                "bankName": "",
                "effectiveFrom": "2023-06-24T00:00:00Z",
                "createdOn": "2023-06-24T23:58:41Z"
            }
        ],

In the first response I am expecting bankName to be empty; in the second I am expecting it to be populated. As I am testing 1000s of responses I want to be able to ignore the bankName from the comparison.

Here’s how I am comparing:

pm.collectionVariables.set("response2", JSON.stringify(pm.response.json())); 

const firstResponse = pm.collectionVariables.get("response1");
const secondResponse = pm.collectionVariables.get("response2");

// might need to remove all instances of bankName from both responses as I'm now passing the bankName in the stored procedure

const _ = require('lodash');

const r1 = _.sortBy(firstResponse); 
const r2 = _.sortBy(secondResponse); 

pm.test("V1 response should be equal to V2 response", function () {
    pm.expect(r1).to.deep.equal(r2);
});

My test runs 1, followed by 2.

Please be gentle with me :slight_smile:

Doing a diff on two JavaScript object when you have complex JSON is not straight forward.

Now that you can use external libraries in Postman, you may be able to find a library that can perform the differential.

Other options to consider.

  1. Just delete the bank name key.
    How do I remove a key from a JavaScript object? - Stack Overflow
    You have multiple bank names in an array, so this in itself will probably need some sort of loop.
  2. Depending on what you are actually testing here, you could use JSONSchema to validate the schema. You can just make the bank name optional and everything else mandatory. If you are testing for the existence of the keys, then this is probably the most correct way of doing this. But JSONSchema itself is a bit of a learning curve.

Your sort is not going to work the way you think it is. It will only sort the top level of the object. It’s not going to sort the bankDetails array.

Thanks Mike. The sort is doing enough to ensure the arrays are ordered. I’ve now agreed with the solution provider to blank the bank name until I have completed the comparison testing.