It's not too hard to create a function like this. Just loop through each field in the second object, and if it's not present in the first or the value is different than the first, put that field in the return object.

var compareJSON = function(obj1, obj2) {
  var ret = {};
  for(var i in obj2) {
    if(!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
      ret[i] = obj2[i];
    }
  }
  return ret;
};

You can see it in action on this demo page.

Answer from Peter Olson on Stack Overflow
🌐
npm
npmjs.com › package › json-diff-ts
json-diff-ts - npm
Modern TypeScript JSON diff library - json-diff-ts is a lightweight, high-performance TypeScript library for calculating and applying differences between JSON objects.
      » npm install json-diff-ts
    
Published   Mar 06, 2026
Version   4.10.0
Author   Christian Glessner
🌐
GitHub
github.com › thiagomayrink › json-diff-ts-cjs
GitHub - thiagomayrink/json-diff-ts-cjs: A JSON diff tool for JavaScript written in TypeScript. forked from: https://github.com/ltwlf/json-diff-ts.git
A JSON diff tool for JavaScript written in TypeScript. forked from: https://github.com/ltwlf/json-diff-ts.git - thiagomayrink/json-diff-ts-cjs
Author   thiagomayrink
🌐
GitHub
github.com › ltwlf › json-diff-ts
GitHub - ltwlf/json-diff-ts: A diff tool for JavaScript written in TypeScript. · GitHub
Modern TypeScript JSON diff library - json-diff-ts is a lightweight, high-performance TypeScript library for calculating and applying differences between JSON objects.
Starred by 182 users
Forked by 27 users
Languages   TypeScript 98.7% | JavaScript 1.3%
🌐
CodeSandbox
codesandbox.io › examples › package › json-diff-ts
json-diff-ts examples - CodeSandbox
Calculate and apply differences between JSON objects with advanced features like key-based array diffing, JSONPath support, and atomic changesets.301,585Weekly Downloads
🌐
CodeSandbox
codesandbox.io › s › json-diff-ts-2tx774
json-diff-ts - CodeSandbox
August 31, 2023 - Modern TypeScript JSON diff library - Zero dependencies, high performance, ESM + CommonJS support. Calculate and apply differences between JSON objects with advanced features like key-based array diffing, JSONPath support, and atomic changesets.
Published   Aug 31, 2023
🌐
npm
npmjs.com › package › json-diff-kit
json-diff-kit - npm
A better JSON differ & viewer library written in TypeScript.
      » npm install json-diff-kit
    
Published   Mar 03, 2026
Version   1.0.35
Author   Rex Zeng
🌐
GitHub
github.com › RexSkz › json-diff-kit
GitHub - RexSkz/json-diff-kit: A better JSON differ & viewer, support LCS diff for arrays and recognise some changes as "modification" apart from simple "remove"+"add". · GitHub
A better JSON differ & viewer library written in TypeScript.
Starred by 209 users
Forked by 17 users
Languages   TypeScript 92.7% | Less 4.7% | JavaScript 2.6%
Find elsewhere
🌐
GitHub
github.com › ltwlf › json-diff-ts › blob › master › .prettierrc
json-diff-ts/.prettierrc at master · ltwlf/json-diff-ts
A diff tool for JavaScript written in TypeScript. Contribute to ltwlf/json-diff-ts development by creating an account on GitHub.
Author   ltwlf
🌐
GitHub
github.com › ltwlf › json-diff-ts › issues
ltwlf/json-diff-ts
A diff tool for JavaScript written in TypeScript. Contribute to ltwlf/json-diff-ts development by creating an account on GitHub.
Author   ltwlf
🌐
JSON Diff
jsondiff.com
JSON Diff - The semantic JSON compare tool
Validate, format, and compare two JSON documents. See the differences between the objects instead of just the new lines and mixed up properties.
🌐
npms
npms.io › search
JSON diff
npms was built to empower the javascript community by providing a better and open sourced search for node modules.
🌐
npm
npmjs.com › package › @types › json-diff
@types/json-diff - npm
TypeScript definitions for json-diff. Latest version: 1.0.3, last published: 2 years ago. Start using @types/json-diff in your project by running `npm i @types/json-diff`. There are 14 other projects in the npm registry using @types/json-diff.
      » npm install @types/json-diff
    
🌐
Js
json-diff-kit.js.org
JSON Diff Kit Playground
We cannot provide a description for this page right now
🌐
Cloudsmith
cloudsmith.com › navigator › npm › @types › json-diff
@types/json-diff (1.0.3) - npm Package Quality | Cloudsmith Navigator
Learn all about the quality, security, and current maintenance status of @types/json-diff using Cloudsmith Navigator
🌐
jsDelivr
jsdelivr.com › package › npm › json-diff-ts
json-diff-ts CDN by jsDelivr - A CDN for npm and GitHub
November 30, 2019 - Modern TypeScript JSON diff library - Zero dependencies, high performance, ESM + CommonJS support. Calculate and apply differences between JSON objects with advanced features like key-based array diffing, JSONPath support, and atomic changesets.
Published   Nov 30, 2019
🌐
npm
npmjs.com › package › json-diff
json-diff - npm
% json-diff --help Usage: json-diff [-vCjfonskKp] first.json second.json Arguments: <first.json> Old file <second.json> New file General options: -v, --verbose Output progress info -C, --[no-]color Colored output -j, --raw-json Display raw JSON encoding of the diff -f, --full Include the equal sections of the document, not just the deltas --max-elisions COUNT Max number of ...s to show in a row in "deltas" mode (before collapsing them) -o, --output-keys KEYS Always print this comma separated keys, with their value, if they are part of an object with any diff -x, --exclude-keys KEYS Exclude these comma separated keys from comparison on both files -n, --output-new-only Output only the updated and new key/value pairs (without marking them as such).
      » npm install json-diff
    
Published   May 15, 2023
Version   1.0.6
Author   Andrey Tarantsov
🌐
Json-format
json-compare.json-format.com › home › uncategorized › json diff, json compare, diff, json-diff-ts
JSON Diff, JSON Compare, Diff, JSON-Diff-TS - online json comparator
November 26, 2025 - A JSON diff tool, such as JSON-Diff-TS, helps developers compare JSON files and JSON objects efficiently. Using a compare tool, you can analyze compare data, detect diff data, and visualize changes in semantic JSON. These tools allow you to validate JSON documents, review keys and paths, track ...
🌐
Deno
deno.com › npm › package › json-diff-ts
Use the json-diff-ts npm package in Deno
Deno supports npm. You can install json-diff-ts with $ deno install npm:json-diff-ts
Top answer
1 of 9
63

It's possible to use a recursive function that iterates by the object keys. Then use the Object.is to test for NaN and null. Then test if the second object is the type that cast to false like 0, NaN, or null. List the keys of both objects and concatenate them to test of missing keys in the obj1 and then iterate it.

When there is a difference between the same key values, it stores the value of object2 and proceeds. If both key values are object means that can be recursively compared and so it does.

function diff(obj1, obj2) {
    const result = {};
    if (Object.is(obj1, obj2)) {
        return undefined;
    }
    if (!obj2 || typeof obj2 !== 'object') {
        return obj2;
    }
    Object.keys(obj1 || {}).concat(Object.keys(obj2 || {})).forEach(key => {
        if(obj2[key] !== obj1[key] && !Object.is(obj1[key], obj2[key])) {
            result[key] = obj2[key];
        }
        if(typeof obj2[key] === 'object' && typeof obj1[key] === 'object') {
            const value = diff(obj1[key], obj2[key]);
            if (value !== undefined) {
                result[key] = value;
            }
        }
    });
    return result;
}

The code above is BSD licensed and can be used anywhere.

Test link: https://jsfiddle.net/gartz/vy9zaof2/54/

An important observation, this will convert arrays to objects and compare the values in the same index position. There are many other ways to compare arrays not covered by this function due to the required extra complexity.

EDIT 2/15/2019: This answer was changed to add the new ES2017 syntax and fix use-cases from comments.


This is just a kickoff, I haven't tested it, but I began with a filter or comparator function, that is recursive, change it however you need to get priority results.

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;
}

Tests: http://jsfiddle.net/gartz/Q3BtG/2/

2 of 9
11

contributing back my changes to Gabriel Gartz version. This one works in strict mode and removes the array check - will always be false. It also removes empty nodes from the diff.

//http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
var isEmptyObject = function(obj) {
    var name;
    for (name in obj) {
        return false;
    }
    return true;
};

//http://stackoverflow.com/questions/8431651/getting-a-diff-of-two-json-objects
var diff = function(obj1, obj2) {
    var result = {};
    var change;
    for (var key in obj1) {
        if (typeof obj2[key] == 'object' && typeof obj1[key] == 'object') {
            change = diff(obj1[key], obj2[key]);
            if (isEmptyObject(change) === false) {
                result[key] = change;
            }
        }
        else if (obj2[key] != obj1[key]) {
            result[key] = obj2[key];
        }
    }
    return result;
};