0

I have 2 Json's object (this is an exemple, my object can have array in array in object, etc....):

var obj = jQuery.parseJSON('{
    "name" : "John",
    "lastname" : "Doe",
    "birthday" : "05/20/1980",
    "fav_game" : [
        "tetris",
        "tic tac toe"
    ],
    "best_friends" : {
        "name" : "Jane",
        "lastname" : "Murdoc"
    }
}');

var obj2 = jQuery.parseJSON('{
    "name" : "John",
    "lastname" : "Doe_update",
    "birthday" : "05/20/1980",
    "fav_game" : [
        "tetris",
        "tic tac toe_update"
    ],
    "best_friends" : {
        "name" : "Jane",
        "lastname" : "Murdoc",
        "new_prop" : "update"
    }
}');

I would like get a difference between this 2 object for create a new one :

var obj3 = diff(obj, obj2);

Obj3 should looks like it :

{
    "lastname" : "Doe_update",
    "fav_game": [
        null, 
        "tic tac toe_update"
    ],
    "best_friends" : {
        "new_prop" : "update"
    }
}

I found a script and i'm trying to modificate it for my need, but i'm not good enougth :

function filter(obj1, obj2) {
    var result = {};
    for(key in obj1) {

        if(obj2[key] != obj1[key]) {

            result[key] = obj2[key];

        }
        if(typeof obj2[key] == 'array' && typeof obj1[key] == 'array') {
            result[key] = arguments.callee(obj1[key], obj2[key]);

        }
        if(typeof obj2[key] == 'object' && typeof obj1[key] == 'object') {
            result[key] = arguments.callee(obj1[key], obj2[key]);

        }
    }
return result;
}

And the result using the script is :

{
    "lastname" : "Doe_update",
    "fav_game": [
        null, 
        "tic tac toe_update"
    ],
    "best_friends" : {
    }
}

I need to deal with added value...

I see many things by searching, but never the same thing of what i need. Sorry for my low level english, update are welcome. Thanks you guys !

Jax Teller
  • 1,447
  • 2
  • 15
  • 24
  • Check this link..[http://stackoverflow.com/questions/1200562/difference-in-json-objects-using-javascript-jquery] – Muthu Sundar Arumugavelu Jun 01 '16 at 07:31
  • They doesn't deal with array in object, they just have basic object, no deep level. That can't work for me. – Jax Teller Jun 01 '16 at 07:37
  • Possible duplicate of http://stackoverflow.com/questions/37530656/objects-deep-nested-child-level-comparison/37531516#37531516 – George Karanikas Jun 01 '16 at 07:38
  • There are lot's of json diff algorithms avalialble, please cite the ones you already tried. – hazardous Jun 01 '16 at 08:00
  • i tried http://jsfiddle.net/gartz/Q3BtG/2/ this one, but it doesn't go deep, it can't get difference on object in object – Jax Teller Jun 01 '16 at 08:02
  • [This one](http://stackoverflow.com/questions/8572826/generic-deep-diff-between-two-objects) can certainly be used for your case: it gives more detailed information, and turns arrays into objects, but that should be easy to adapt. – trincot Jun 01 '16 at 08:25

1 Answers1

0

You may try using hash to validate whether the value is the same.

This requires 3rd party plugin like jsHashes.

// Check the documentation for diffrent type of hashes
var SHA256 = new Hashes.SHA256;

var a = {
  a: 1,
  b: 2
};

var b = {
  a: 1,
  b: 2
}

var c = {
  a: 1,
  b: c
}

console.log(isMatch(a, b));
console.log(isMatch(a, c));
console.log(isMatch(b, c));
console.log(isMatch(b, a));


function isMatch(a, b) {
  a = typeof a !== 'undefined' ? a : [];
  b = typeof b !== 'undefined' ? b : [];

  if (SHA256.hex(JSON.stringify(a)) === SHA256.hex(JSON.stringify(b))) {
    return true;
  }

  return false;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jshashes/1.0.6/hashes.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>