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.