Assert two JSON objects

hey i want to assert two JSON objects , can anyone help me in this.

Example:
var jsonData = JSON.stringify(responseBody)

var expectedObject = {
“name” : “john”,
“id” : “doe”
}

Like this i have two JSON objects , one which i have taken from response and another is expected one which i am preparing in my tests script and i want to match these two objects two check whether they are same or not.

Hello @himanshudwivedi1989, welcome to the community! :wave:

You can use to.eql and write the following:

pm.test('Check if objects are equal', function () {
 pm.expect(jsonData).to.eql(expectedObject);
});
3 Likes

Hi Siv
thanks for the quick help.

1 Like

Hi,
This is somehow not working for me.
In case:-
var expectedObject = {
“id”:“doe”,
“name”:“john”
}
i.e. the order of the fields inside the json structure changes then the assertion fails.
If its actually a JSON object comparision then it should not fail.
Anyway, this can be achieved?

1 Like

Any solution found to the key re-order problem?