# How to compare two json objects ignoring the order of fields

**URL:** <https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584>\
**Category:** Help Hub\
**Tags:** tests, collections, json, api\
**Created:** [October 23, 2023, 7:03am UTC](https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584 "2023-10-23T07:03:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ku900](https://avatars.discourse-cdn.com/v4/letter/k/8e8cbc/32.png) [@ku900](https://community.postman.com/u/ku900)\
**Post date:** [October 23, 2023, 7:03am UTC](https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584/1 "2023-10-23T07:03:20Z")

</div>

Hello everyone,

I’m passing a request body with a JSON object containing n number of fields.

The body format is included in the body and this specific JSON object is in the pre-requisite script.

Upon hitting the post request an ID gets generated.

Now, I use get API with this ID to fetch the response.

Till now it’s all good.

As part of the test, I need to validate the response of the get API containing the JSON node that is passed as part of the prerequisite script.

methods that I have tried:

prerequisite step -

var employeedetails = { “id” = “12”, “location” = “USA”}

pm.expect(pm.response.text()).to.include(employeedetails)

this fails because in the response body, the employeedetails JSON object displays the fields in a different order like

response  
{  
“body”: xxxxx,  
“employeedetails”:  
{  
“location” = “USA”,  
“id” = “12”,  
}  
}

the second method that I have tried is  
extracting this specific node from the response and comparing it with the node passed in the request.

like  
var jsonData = JSON.parse(responsebody);  
var response = jsonData.employeedetails;  
var responseNode = JSON.stringify(response)

.\_equals (employeedetails, responseNode)

this returns true even when the fields are missing/different.

What I’m looking for is  
the comparison should verify the count of elements in the request vs response matching  
the key-value pairs are matching, regardless of the order in the request vs response

only when these conditions are true, does the function have to return true.

Your help is highly appreciated!! 🙂

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [October 23, 2023, 10:19am UTC](https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584/2 "2023-10-23T10:19:23Z")

</div>

You can’t guarantee the order of the keys in an JavaScript object.

Therefore you will need to create an array of the keys, which you can then sort before you make the comparison.

Consider the following example…

```auto
var employeedetails = { "id": "12", "location": "USA" };

const response = pm.response.json().employeedetails;

let expectedResult = Object.keys(employeedetails).sort();
console.log(expectedResult);
let actualResult = Object.keys(response).sort();
console.log(actualResult);

pm.test("response keys match", function () {
    pm.expect(actualResult).to.eql(expectedResult);
});

```

You might also want to consider using JSONSchema to validate the JSON structure.

[Schema validation — Postman Quick Reference Guide Version 1.9.1 - January 2023 documentation (postman-quick-reference-guide.readthedocs.io)](https://postman-quick-reference-guide.readthedocs.io/en/latest/schema-validation.html)

---

<div class="post-metadata">

**Author:** ![dmiff](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/dmiff/32/26448_2.png) [@dmiff](https://community.postman.com/u/dmiff)\
**Post date:** [November 6, 2023, 4:32pm UTC](https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584/3 "2023-11-06T16:32:56Z")

</div>

The “expect” functionality is part of the [Chai Assertion Library](https://www.chaijs.com/api/bdd/).

The [deep equal](https://www.chaijs.com/api/bdd/#method_deep)" option will test for equality without regard to field order.

pm.expect(pm.response.text()).to.deep.include(employeedetails)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 7, 2023, 11:50am UTC](https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584/4 "2023-11-07T11:50:38Z")

</div>

@dmiff

Learn something every day. Something to remember in the future.

```auto
var employeedetails = { "id": "12", "location": "USA" };

const response = pm.response.json().employeedetails;

pm.test("response keys match - deep includes", function () {
    pm.expect(response).to.deep.include(employeedetails);
});

```

I tested this by changing the keys and values, and it appears to work correctly.

At which point I noticed that my earlier post was not working correctly, as it was only checking the key names. 🫢

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 7, 2023, 11:50am UTC](https://community.postman.com/t/how-to-compare-two-json-objects-ignoring-the-order-of-fields/53584/5 "2023-12-07T11:50:49Z")

</div>

Closed on December 7, 2023, 11:50am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
