Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I compare two JSON objects and return new JSON object with only the changes and if there is empty data in the OldData in a key then it will show the data of newData see the example below:

OldData:

JavaScript
{
   "_id":"3fad6024-3226-451b-9e81-1c544aaaebf7",
   "name":"ank agents",
   "aboutUs":"i am about us",
   "agents":[],
   "retailerLanguage":[
      {
         "languageID":"20b4772c-2470-4eaa-bc0c-61429700781c",
         "language":{
            "name":"Korean",
            "__typename":"language"
         }
      },
      {
         "languageID":"9g04da56-0f53-4694-b6dc-0eb5a3aa1988",
         "language":{
            "name":"french",
            "__typename":"language"
         }
      }
   ],
   "termsAndConditions":"not agreed"
   }

NewData:

JavaScript
{
   "_id":"3fad6024-3226-451b-9e81-1c544aaaebf7",
   "name":"ank retailer part 2",
   "aboutUs":"i am about us",
   "agents":[
      {
         "agentID":"89add463-7cb7-442a-b705-405e03f7e86a"
      },
      {
         "agentID":"1c98d888-6c43-463c-b7ed-79ea8736125f"
      }
   ],
 "retailerLanguage":[
 
  {
     "languageID":"20b4772c-2470-4eaa-bc0c-61429700781c", language: {name: "Korean", __typename: "language"}
  },
   {
     "languageID":"8f04da56-0f53-4694-b6dc-0eb5a3aa2990", language: {name: "Mandarin", __typename: "language"}
  },
   ],

   "termsAndConditions":"agreed"
}


Expected output:

JavaScript
{
   "name":"ank retailer part 2",
   "agents":[
      {
         "agentID":"89add463-7cb7-442a-b705-405e03f7e86a"
      },
      {
         "agentID":"1c98d888-6c43-463c-b7ed-79ea8736125f"
      }
   ],
   "retailerLanguage":[
       {
     "languageID":"8f04da56-0f53-4694-b6dc-0eb5a3aa2990", language: {name: "Mandarin", __typename: "language"}
  },
   ],

   "termsAndConditions":"agreed"
}

I have tried and searched the various post in the stackoverflow but couldn't find the suitable example. Thanks in Advance.

What I have tried:

 //Function for comparision start here


  var diffParams = {};

  for( var p in obj1 ){
    if ( !compareValue(obj1[p] && obj1[p],obj2[p]&& obj2[p]) ){
      diffParams[p] = obj1[p];
    }
  }
  
  function compareValue(val1, val2){
    var isSame = true;
    for ( var p in val1 ) {
  
      if (typeof(val1[p]) === "object"){
        var objectValue1 = val1[p],
            objectValue2 = val2[p];
        for( var value in objectValue1 ){
          isSame = compareValue(objectValue1[value], objectValue2[value]);
          if( isSame === false ){
            return false;
          }
        }
      }else{
        if(val1 !== val2){
          isSame = false;
        }
      }
    }
    return isSame;
  }

//Function for comparision end here

var d = diffParams
console.log("the activity 146", d)
Posted
Comments
[no name] 2-Jun-21 12:34pm    
Deserialize the 2 JSON to objects; create a 3rd objects; compare and put differences in object 3; serialize object 3 to JSON.

In your example, you add 2 new "languages" but only expect 1 (?). And you can't account for "deletions".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900