0

I have two object JSON which having hierachical structure. I need to compare each object inside rowset in JSON. And where ever the value not gettng equal then i have to addd one flag on corresponding object.

Please hava a look at my JSONs and give solution for that. If not angular atleast i have to achieve in Javascript.

Thanks in advance...

JSON1

{"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Ranjini","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"Electronic City","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}}

JSON2

{"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Vijay","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"SilkBoard","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}}
bagya
  • 383
  • 1
  • 11
  • 38
  • What do you mean by adding a flag to the corresponding object? – Sara Fuerst May 10 '16 at 12:42
  • 1
    You can compare two objects with `angular.equals(obj1, obj2);`. – Arg0n May 10 '16 at 12:44
  • Possible duplicate of [Compare objects in Angular](http://stackoverflow.com/questions/20409712/compare-objects-in-angular) – JrBenito May 10 '16 at 12:51
  • 1
    @JrBenito - I need to get the exact object value of not matching. – bagya May 10 '16 at 12:56
  • @tibsar - I want exact non match object value. In this example JSON object Name "Customer29Jan16" , The Name attribute different from another JSON . i need to find out the index to add the flag over there. – bagya May 10 '16 at 13:00
  • @Arg0n I want exact non match object value. In this example JSON object Name "Customer29Jan16" , The Name attribute different from another JSON . i need to find out the index to add the flag over there – bagya May 10 '16 at 13:01

2 Answers2

6

Use angular.equals(o1,o2) . It does deep comparison and does not depend on the order of the keys

angular.equals(JSON1, JSON2); // return boolean (true or false) based on the comparison 

refer : Angularjs Docs

Shushanth Pallegar
  • 2,832
  • 1
  • 14
  • 16
  • It will give either true or false. I want exact non match object value. In this example JSON object Name "Customer29Jan16" , The Name attribute different from another JSON . i need to find out the index to add the flag over there. I hope you're getting my requirement. – bagya May 10 '16 at 12:59
  • @bagya check out this: http://stackoverflow.com/questions/4465244/compare-2-json-objects You may be able to modify it to meet your needs – Sara Fuerst May 10 '16 at 13:12
3

I've made an starting example for you to build on. You can play with it here.

JavaScript

var json1 = {"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Ranjini","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"Electronic City","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}};

var json2 = {"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Vijay","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"SilkBoard","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}};

function compareJSON(json1, json2) {
  var objectsDiffering = [];
  compareJSONRecursive(json1, json2, objectsDiffering);
  return objectsDiffering;
}

function compareJSONRecursive(json1, json2, objectsDiffering) {
  for(prop in json1) {
    if(json2.hasOwnProperty(prop)) {
        switch(typeof(json1[prop])) {
        case "object":
            compareJSONRecursive(json1[prop], json2[prop], objectsDiffering);
          break;
        default:
          if(json1[prop] !== json2[prop]) {
            objectsDiffering.push(json1);
          }
          break;
      }
    } 
    else {
      objectsDiffering.push(json1);
      break;
    }
  }
}

var differing = compareJSON(json1, json2);
console.log(JSON.stringify(differing));
//Logs: [{"CuId":"123","Name":"Ranjini","Quantity":"60","Rate":"60","Amount":"3600"},{"CuObjId":"456","FullAddress":"Electronic City","ObjAddr":"Bangalore","ObjName":"Testing"}]
Arg0n
  • 8,283
  • 2
  • 21
  • 38
  • Thanks for your code. Now i have small issue in this. When ever the existing object properties not matching i need to add "operationContext: IsUpdate". The same time whole object not there in JSON1 and is there in JSON2 in that case i need to add "operationContext: IsAdded". I hope you're getting my point. – bagya May 11 '16 at 07:50
  • @bagya Add this before `objectsDiffering.push(json1);`. You want to set a property on the object right? – Arg0n May 11 '16 at 08:02
  • yes..please have look on fiddle. https://jsfiddle.net/bagya1985/y9efgzh6/2/. This fiddle is working for matching object change. suppose Non-matching object. Where i need to include ""operationContext: IsAdded". – bagya May 11 '16 at 09:14
  • @bagya You are setting the property on `json2`, `json1` is the object getting added. Maybe you want to switch this somehow? – Arg0n May 11 '16 at 10:54
  • Here i newly added object property in JSON2. I can make it JSON1 or JSON2 that is no issue. but how can i add "operationContext: IsAdded" for newly added object only. please have look my Fiddle link https://jsfiddle.net/bagya1985/y9efgzh6/2/ and JSON2 – bagya May 11 '16 at 11:02
  • Please check and give solution for that...Thanks in advance – bagya May 11 '16 at 14:05
  • Please have look my fiddle https://jsfiddle.net/bagya1985/y9efgzh6/3/. I need to add "operationContext: IsAdded" for newly added object only. – bagya May 12 '16 at 06:39
  • @bagya I understand that, but since i don't know the logic of your code it would be better if you added this yourself. I gave you code to build on, you have to do some of the work yourself... – Arg0n May 12 '16 at 06:48