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":[]}}}
6
  • What do you mean by adding a flag to the corresponding object? Commented May 10, 2016 at 12:42
  • 1
    You can compare two objects with angular.equals(obj1, obj2);. Commented May 10, 2016 at 12:44
  • Possible duplicate of Compare objects in Angular Commented May 10, 2016 at 12:51
  • 1
    @JrBenito - I need to get the exact object value of not matching. Commented May 10, 2016 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. Commented May 10, 2016 at 13:00

2 Answers 2

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

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

2 Comments

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 check out this: stackoverflow.com/questions/4465244/compare-2-json-objects You may be able to modify it to meet your needs
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"}]

8 Comments

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 Add this before objectsDiffering.push(json1);. You want to set a property on the object right?
yes..please have look on fiddle. 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 You are setting the property on json2, json1 is the object getting added. Maybe you want to switch this somehow?
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 jsfiddle.net/bagya1985/y9efgzh6/2 and JSON2
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.