I have a JSON files which I want to compare the values and ensure both of the JSON is having the same values and return TRUE if there is any one of the value is not available then I need to return immediately FALSE.
The problem is now I'm having the two JSON files with Same Values but the row position is different.
JSON_1:
{
"sqlQuery": "select type,subtype from wetrade_p2 where parent_id='69341269'",
"message": "2 rows selected",
"row": [
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "P",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
},
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "S",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
}
]
}
JSON_2:
{
"sqlQuery": "select type,subtype from wetrade_p2 where parent_id='69341269'",
"message": "2 rows selected",
"row": [
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "S",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
},
{
"column": [
{
"value": "W",
"name": "TYPE"
},
{
"value": "P",
"name": "STATUS"
},
{
"value": "0",
"name": "SUBTYPE"
},
{
"value": "USD",
"name": "CURRENCY"
}
]
}
]
}
If you check the above JSON_1 and JSON_2 and both are having same values and but the ROW is mismatched/ JSON objects order is not remain same.Because of this the below code is return FALSE even thought the values are same:
Code:
compareActualAndExpectedResponse:function (jsonActual,jsonExpected) {
var jsonObject1 = JSON.parse(jsonActual);
var jsonObject2 = JSON.parse(jsonExpected);
var keys = Object.keys(jsonObject1);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (jsonObject1[key] != jsonObject2[key]) {
console.log(key + " value changed from '" + jsonObject1[key] + "' to '" + jsonObject2[key] + "'");
return false;
}
}
return true;
}
UpdatedCode:
Utils.js:
var methods = {
getuuid:function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
},
compareActualAndExpectedResponse:function (jsonActual, jsonExpected) {
if (Object.prototype.toString.call(jsonActual) !==
Object.prototype.toString.call(jsonExpected)) {
console.log("false 1");
return false;
}
if (Array.isArray(jsonActual)) {
if (jsonActual.length !== jsonExpected.length) {
console.log("false 2")
return false;
}
let i = jsonActual.length;
while (i--) {
let j = jsonExpected.length;
let found = false;
while (!found && j--) {
if (compareActualAndExpectedResponse(jsonActual[i], jsonExpected[j])) found = true;
}
if (!found) {
console.log("false 3");
return false;
}
}
console.log("true 1")
return true;
}
if (Object.prototype.toString.call(jsonActual) === '[object Object]') {
for (const key in jsonActual) {
if (Object.prototype.hasOwnProperty.call(jsonActual, key)) {
if (!Object.prototype.hasOwnProperty.call(jsonExpected, key)) {
console.log("false 4");
return false;
}
if (!compareActualAndExpectedResponse(jsonActual[key], jsonExpected[key])) {
console.log("false 5")
return false;
}
}
}
for (const key in jsonExpected) {
if (Object.prototype.hasOwnProperty.call(jsonExpected, key) &&
!Object.prototype.hasOwnProperty.call(jsonActual, key)) {
console.log("false 6")
return false;
}
}
console.log("true 2")
return true;
}
return jsonActual === jsonExpected;
}
};
module.exports = methods;
Client.JS:
var utils = require('./Utils');
if(key.includes("entireJSONResponse"))
{
var data=fs.readFileSync(global.path+"/validationJSON/JSONResponseValidator/Bank.json",'utf8')
data=data.replace("{buyer}",transresult.get("buyeraccountnumber"));
const expected=JSON.stringify(transresult.get(key),null,4);
console.log(expected)
console.log(data);
var validationResult=utils .compareActualAndExpectedResponse(data,expected);
console.log(validationResult); //Still return false
console.log(utils.compareActualAndExpectedResponse(dataFinal,expected));
console.log("*************************************")
}