How to Compare Two Json Objects Excluding the Fields That Are Specified in a Separate Array (Postman Script)

How to compare two JSON objects excluding the fields that are specified in a separate array?(Postman Script)

Json Object 1: keysave,valuesave
Json Object 2: keylist,valuelist
//values that aren't need to be checked
exclude = ["id","qty","isActive"]

Code:

 var ex1=pm.environment.get("exclude");
var resp=[];
for (var i in keysave)
{
console.log(keysave[i]);
if(ex1.indexOf(keysave[i]) < 0)
{
var flag =0;
for (var j in keylist)
{
if(keylist[j] === keysave[i] && valuelist[j] === valuesave[i])
{
flag = 0;
break;
}
else
{
flag = 1;

}
}
if(flag === 0)
{
console.log("Matched value "+keysave[i]);
}
else
{
console.log("None matched value "+keysave[i]);
resp.push(keysave[i])
console.log(resp);
}
}
}
if(resp.length > 0)
{
tests[resp] = false;
}
else
{
tests['Both JSON are Equal'] = true;
}

Postman - Compare two responses ignoring order and some specific properties

I've figured out a solution. On pre-request script:

pm.globals.set("loadAsserts", function loadAsserts(){
let asserts = {};
let jsonData = JSON.parse(responseBody);

asserts.areEqual = function areEqual(actual, expected, objectName) {
pm.test(`Actual ${objectName} '` + actual + `' matches Expected ${objectName} '` + expected + `'`, () => {
pm.expect(_.isEqual(actual, expected)).to.be.true;
});
}

asserts.areArrayOfObjectsEqual = function areArrayOfObjectsEqual(actual, expected, objectName) {
if (!_.isEqual(actual, expected)) {

// Arrays are not equal so report what the differences are
for (var indexItem = 0; indexItem < expected.length; indexItem++) {
assert.compareArrayObject(actual[indexItem], expected[indexItem], objectName);
}
}
else
{
// This fake test will always pass and is just here for displaying output to highlight that the array has been verified as part of the test run
pm.test(`actual '${objectName}' array matches expected '${objectName}' array`);
}
}

asserts.compareArrayObject = function compareArrayObject (actualObject, expectedObject, objectName) {
for (var key in expectedObject) {
if (expectedObject.hasOwnProperty(key)) {
assert.areEqual(expectedObject[key], actualObject[key], objectName + " - " + key);
}
}
}

return asserts;
} + '; loadAsserts();');

tests['Asserts initialized'] = true;
pm.globals.set("actualResponse", pm.response.json());`

Now, when I want to call it in a response:

asserts.areEqual(actualResponse.id, expectedResponse.id, "id");

Parsing nested, "named" objects in Postman

You would need to walk down the structure to get the value that you need. As there are objects and arrays to navigate, you would need to reference these correctly.

You also have property names with dots and dashes in them so you would need to use a mixture of dot and bracket notation to correctly reference those.

This should log that guide value in the console:

let responseInJson = pm.response.json(),
workFlowData = responseInJson.Workflow["com.mycom.MyWorkflow"].stages[0]["com.mycom.MyStage"]["stage-guid"];

console.log(workFlowData);

Postman get value from JSON where equals a value in array using javascript

You can use: Array.prototype.find():