I have two json
objects,
Object 1
{
"ClassificationId": 1,
"ClassificationSort": 40,
"ClassificationType": 146,
"IsActive": true,
"Level": 0,
"LongName": "XYZ",
"ShortName": "XYZ"
}, {
"ClassificationId": 2,
"ClassificationSort": 45,
"ClassificationType": 146,
"IsActive": true,
"Level": 0,
"LongName": "ABC",
"ShortName": "ABC"
}, {
"ClassificationId": 3,
"ClassificationSort": 50,
"ClassificationType": 146,
"IsActive": true,
"Level": 0,
"LongName": "DEF",
"ShortName": "DEF"
}
Object 2
{
"ClassificationId": 1,
"ClassificationSort": 40,
"ClassificationType": 146,
"IsActive": true,
"Level": 0,
"LongName": "XYZ_1",
"ShortName": "XYZ"
}, {
"ClassificationId": 2,
"ClassificationSort": 45,
"ClassificationType": 146,
"IsActive": true,
"Level": 0,
"LongName": "ABC_1",
"ShortName": "ABC"
}, {
"ClassificationId": 3,
"ClassificationSort": 50,
"ClassificationType": 146,
"IsActive": true,
"Level": 0,
"LongName": "DEF_1",
"ShortName": "DEF"
}
I want to compare both objects
to find out which key
has changed. I need the changed key
and value
See snippet below for what ive tried so far. Its however not working as expected.
var result = JSON.parse(json1);
var result2 = JSON.parse(json2);
$.each(result, function (k, v) {
var key1 = k;
var value1 = v;
$.each(result2, function (k2, v2) {
if (key1 == k2) {
if (value1 != v2)
//console.log("key:" + k2 + "value:" + v2);
changeData = "[{\"ChangedColumn\":\"" + key1 + "\",\"ChangedValueOld\":\"" + value1 + "\",\"ChangedValueNew\":\"" + v2 + "\",\"RowIdentifierValue\":\"140\"}],";
//console.log(changeData);
return false;
}
});
});
Any help will be appreciated.