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 !

6
  • Check this link..[stackoverflow.com/questions/1200562/… Commented Jun 1, 2016 at 7:31
  • They doesn't deal with array in object, they just have basic object, no deep level. That can't work for me. Commented Jun 1, 2016 at 7:37
  • Possible duplicate of stackoverflow.com/questions/37530656/… Commented Jun 1, 2016 at 7:38
  • There are lot's of json diff algorithms avalialble, please cite the ones you already tried. Commented Jun 1, 2016 at 8:00
  • i tried jsfiddle.net/gartz/Q3BtG/2 this one, but it doesn't go deep, it can't get difference on object in object Commented Jun 1, 2016 at 8:02

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.