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():
const data = {
"Customers": [{
"id": 24,
"userid": 73063,
"username": "BOB",
"firstname": "BOB",
"lastname": "LASTNAME"
},
{
"id": 25,
"userid": 73139,
"username": "Billy",
"firstname": "Billy",
"lastname": "lasty"
}
]
}
const user = data.Customers.find(u => u.username === 'Billy')
const userid = user ? user.userid : 'not found'
console.log(user)
console.log(userid)
Related Topics
How to Change Text Value Upon Button Press in React Native
Allow Negative/Positive Numbers But Not Letters
How to Output Date in JavaScript in Iso 8601 Without Milliseconds and With Z
Set State Twice in a Single Function - Reactjs
How to Change Data Due to Selected Value in Select Vue
How to Share and Edit Global Variables Between Two JavaScript Node Runs
Passing Variable from One Js to Another Js File in Nodejs
How to Hide the Address Bar on Iphone
How Exactly Use and Update the Global Variables in React-Native
Passing Multiple Parameters from Angular to Rest Service
How to Find Middle Element of Array in JavaScript
Convert JavaScript Date Format to Yyyy-Mm-Ddthh:Mm:Ss
Convert 12-Hour Hh:Mm Am/Pm to 24-Hour Hh:Mm
How to Check If Input File Is Empty in Jquery
How to Disable Dates Before Today in Jquery Datepicker