I’m running a test to compare expected json response with actual json response from the application. As part of the test, I want to remove a node whose values are dynamic. During each iteration, the dynamic content will change and will fail the json compare. I would like to know if we have any method which we can use to do selective comparison.
Below script, compare validates the entire json. But need a method to remove a few of the dynamic nodes before doing the compare.
var accounts = data.accounts;
var expJSON = data.expectedresponse;
//JSON Compare step
pm.test("jsonCompare", function () {
var jsonData = pm.response.json();
pm.expect(jsonData).to.eql(expJSON);
});
I can use below approach for testing Block by block, but having 1000’s of nodes to compare, the below approach is not practical.
Hi Danny,
Thank you for the response.
Sample:
Data 1:
{
“id”: “abcd4234232242”,
“Example1”: “this is a test”,
“SessionId”: 2d3d3d34342323334,
“questions”: [
{
“id”: “abcd4234232242"”,
"
}
]
}
Data 2:
{
“id”: “abcd4234232243”,
“Example1”: “this is a test”,
“SessionId”: 2d3d3d3434232335,
“questions”: [
{
“id”: “abcd4234232243"”,
"
}
]
}
Consider id and sessionid has dynamic values.
For now the solution i got from another forum:
function convertToObjectWithExcludeKeys(orginalObject, excludeKeys){
var newObj = {};
Object.keys(orginalObject).map(key => {
if(excludeKeys.indexOf(key) < 0){
newObj[key] = orginalObject[key];
}
})
return newObj;
}
var b = {Sample1}
var a = {Sample2};
var excludeKeys = [“Value1”,“Value2”,“Value3”];
//Convert original objects to temporary objects with exclude keys
var tmpA = convertToObjectWithExcludeKeys(a, excludeKeys);
var tmpB = convertToObjectWithExcludeKeys(b, excludeKeys);