Compare two JSON responses, but ordering of items in them doesn't matter

Hello, I have problem with comparing responses.
Right now Iā€™m using something like below, but that only shows if responses are exactly the same.
In first response I use

pm.globals.set(ā€˜responseā€™, pm.response.json());

And in the second:

pm.test(ā€˜Response the same as previous.ā€™, () => {
pm.expect(pm.response.json()).to.deep.equal(pm.globals.get(ā€˜responseā€™));
});

Sadly in my project there are sometimes responses with different ordering, which according to PO is not an bug, so wonā€™t be fixed. And that makes my tests fails.

Example what I need:
Lets say I have two json responses:

[
{
ā€œletterā€: ā€œaā€,
ā€œexampleā€: ā€œAgaveā€,
ā€œusefulnessā€: ā€œAwesomeā€
},
{
ā€œletterā€: ā€œbā€,
ā€œexampleā€: ā€œBeerā€,
ā€œusefulnessā€: ā€œBest letter everā€
},
{
ā€œletterā€: ā€œcā€,
ā€œexampleā€: ā€œChipsā€,
ā€œusefulnessā€: ā€œCoolā€
}
]

And

[
{
ā€œletterā€: ā€œaā€,
ā€œexampleā€: ā€œAgaveā€,
ā€œusefulnessā€: ā€œAwesomeā€
},
{
ā€œletterā€: ā€œcā€,
ā€œexampleā€: ā€œChipsā€,
ā€œusefulnessā€: ā€œCoolā€
},
{
ā€œletterā€: ā€œbā€,
ā€œexampleā€: ā€œBeerā€,
ā€œusefulnessā€: ā€œBest letter everā€
}
]

And I would like to create a test that would consider those responses as passed.

Hey @konrad.rafalko

Welcome to the Postman community! :trophy:

You could take a look at using the ChaiJS .deep.members() assertion?

I added this to the Tests tab to try it out under different scenarios (Object orders, Property ordering in the Object, Remove properties, Changing key/value names etc.)

let dataSetOne = [
    {
        "letter": "a",
        "example": "Agave",
        "usefulness": "Awesome"
    },
    {
        "letter": "b",
        "example": "Beer",
        "usefulness": "Best letter ever"
    },
    {
        "letter": "c",
        "usefulness": "Cool",
        "example": "Chips"
    }
]

let dataSetTwo = [
    {
        "letter": "a",
        "example": "Agave",
        "usefulness": "Awesome"
    },
    {
        "letter": "c",
        "example": "Chips",
        "usefulness": "Cool"
    },
    {
        "letter": "b",
        "example": "Beer",
        "usefulness": "Best letter ever"
    }
]
pm.test("Compare Data Sets", () => {
    pm.expect(dataSetOne).to.have.deep.members(dataSetTwo);
})
2 Likes

I used this method to compare responses of two same request but got error:

ā€œCompare Data Sets | AssertionError: expected { Object (token, user_summary, ā€¦) } to be an arrayā€

PFB my test script

var jsonData = pm.response.json();

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.globals.set("token", jsonData.token);

pm.environment.set("schoolId", "jsonData.user_summary.school_id");

const expectedResponse = pm.globals.get("aResponseAsJson");

pm.test("Compare Data Sets", () => {
    pm.expect(jsonData).to.have.deep.members(expectedResponse);
})

tests['School ID check'] = pm.environment.get("schoolID") == jsonData.user_summary.school_id;

How do i compare two responses of same request

Hey @rupal.biyani

What are the 2 things that youā€™re comparing? That might help with the error youā€™re seeing :grin:

That script doesnā€™t really tell the whole story :grin:

Hello,

I need the same thing. However Iā€™m comparing two json with nested objects.
If I apply this on my json response I get "AssertionError: expected { Object (data, dictionaries) } to be an array " because itā€™s an object and not an array.

Could you please help?

Thank you in advance.

Best regards

I am getting this as the error::
[ Array(13) ] to have the same members as [ Array(1) ]

any leads on when and what to do?

Hey @Anumundada

You would need to expand on this and share the full context of what youā€™re doing.

That error message alone doesnā€™t give any insight to what you have done to get to this point.