Notification texts go here Contact Us Buy Now!

Compare two JSON objects and just return another JSON object with only the changes

Compare Two JSON Objects and Return a New JSON Object with Only Changes

Introduction:

In the domain of programming, comparing JSON objects to identify differences and changes is a common task. By implementing a structured approach, we can efficiently compute and present the variations between two JSON objects in a new JSON object.

Implementation:

  1. Initialize Variables:
    • Create two JSON objects, obj1 and obj2, to represent the original and modified JSON data, respectively.
    • Initialize an empty JSON object, obj3, to store the differences between the two objects.

  2. Traverse Object Keys:
    • Utilize a for loop to iterate through the keys of obj1.
    • For each key, check whether obj2 possesses the same key.

  3. Comparison and Storage:
    • If obj2 does not have the current key, it signifies a difference. Store the key and its value from obj1 in obj3.
    • If the values associated with the key in obj1 and obj2 are different, update the value in obj3 with the value from obj2.

  4. Handle Nested Objects:
    • If the values associated with the key are objects, recursively call the comparison function on these nested objects.
    • This ensures that changes within nested objects are also captured and stored in obj3.

  5. Return Result:
    • After processing all keys in obj1, obj3 will contain the differences between the two JSON objects.
    • Return obj3 as the final result.

Code Example:

function compareJSON(obj1, obj2) {
  var obj3 = {};
  for (var key in obj1) {
    if (!obj2.hasOwnProperty(key)) {
      obj3[key] = obj1[key];
    } else if (typeof obj1[key] === "object" && typeof obj2[key] === "object") {
      obj3[key] = compareJSON(obj1[key], obj2[key]);
    } else if (obj1[key] !== obj2[key]) {
      obj3[key] = obj2[key];
    }
  }
  return obj3;
}

var obj1 = {
  "name": "John Doe",
  "age": 30,
  "address": {
    "street": "123 Main Street",
    "city": "Anytown",
    "state": "CA"
  }
};

var obj2 = {
  "name": "Jane Smith",
  "age": 35,
  "address": {
    "street": "456 Elm Street",
    "city": "Anytown",
    "state": "CA"
  }
};

var obj3 = compareJSON(obj1, obj2);
console.log(obj3);

Output:

{
  "name": "Jane Smith",
  "age": 35,
  "address": {
    "street": "456 Elm Street"
  }
}
In this example, we have two JSON objects, obj1 and obj2, with some differences in values and a nested object. The compareJSON function successfully identifies these changes and returns a new JSON object, obj3, containing only the variations. By employing this approach, you can compare two JSON objects and obtain a concise representation of their differences in a new JSON object. This technique proves useful in various scenarios requiring data comparison and change tracking.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.