-1

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.

Red
  • 6,599
  • 9
  • 43
  • 85
Sarat
  • 271
  • 5
  • 22

1 Answers1

1

You simply need to loop over the JSON arrays and compare each key values. There are several ways for this but i have included a simple and understanding way to accomplish this.

This is your JSON Arrays:

var json1 = [{"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"}];

var json2 = [{"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"}];

Here is the javascript code that will give you the difference array printed in the browser console:

var keys = Object.keys(json1[0]);

var changeArray = [];

for(var j=0; j<json1.length; j++){
    var json1Inner = json1[j];
    var json2Inner = json2[j];

    for(var i=0; i<keys.length; i++){
      var key = keys[i], changeTrack = {};
      if(json1Inner[key] !== json2Inner[key]){
         changeTrack['ChangedColumn'] = key;
         changeTrack['ChangedValueOld'] = json1Inner[key];
         changeTrack['ChangedValueNew'] = json2Inner[key];
         changeTrack['RowIdentifierValue'] = '140';

         changeArray.push(changeTrack);
      }
    }
}


console.log(changeArray);

For simplicity, here is the JSFIDDLE

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • @AKA...really thanks for your help...it worked.... Could you please help to get the ClassificationId as RowIdentifierValue here – Sarat Jun 28 '17 at 05:36
  • If you need the ClassificationId of json1 then use changeTrack['RowIdentifierValue'] = json1Inner['ClassificationId'] or if you need for json2 then use changeTrack['RowIdentifierValue'] = json2Inner['ClassificationId'] – Ankit Agarwal Jun 28 '17 at 05:58
  • @AKA....how can i generalize this.....instead of mentioning classification id how can i get the value of first key's value – Sarat Jun 28 '17 at 06:46