Lodash _.isEqual allows you to do that:

var
remoteJSON = {"allowExternalMembers": "false", "whoCanJoin": "CAN_REQUEST_TO_JOIN"},
    localJSON = {"whoCanJoin": "CAN_REQUEST_TO_JOIN", "allowExternalMembers": "false"};
    
console.log( _.isEqual(remoteJSON, localJSON) );
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Answer from Nicolas on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
JSON Editor Online
jsoneditoronline.org โ€บ home โ€บ compare โ€บ json-compare
JSON compare: how to compare two JSON files? | Indepth
January 18, 2023 - In some cases, you only need an ... is one common pitfall to be aware of. In JavaScript, you can for example use the lodash function isEqual, which is very fast....
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-compare
JSON Compare Online to find different between two json
Version Control: When tracking ... added, or removed. JSON Compare refers to the process of comparing two JSON (JavaScript Object Notation) data structures to determine the differences between them....
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ json-diff
json-diff - npm
If you need only the diffs from the old file, just exchange the first and second json. -s, --sort Sort primitive values in arrays before comparing -k, --keys-only Compare only the keys, ignore the differences in values -K, --keep-unchanged-values Instead of omitting values that are equal, output them as they are -p, --precision DECIMALS Round all floating point numbers to this number of decimal places prior to comparison -h, --help Display this usage information ยท In javascript (ES5): var jsonDiff = require('json-diff'); console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' })); // Out
      ยป npm install json-diff
    
Published ย  May 15, 2023
Version ย  1.0.6
Author ย  Andrey Tarantsov
๐ŸŒ
DEV Community
dev.to โ€บ keploy โ€บ diff-json-a-complete-guide-to-comparing-json-data-3e31
Diff JSON โ€“ A Complete Guide to Comparing JSON Data - DEV Community
October 15, 2024 - โ€ข JSON-diff (npm library): This JavaScript package helps compare JSON objects and outputs differences. Itโ€™s widely used in Node.js environments. โ€ข Postman: A popular API testing tool that includes JSON comparison features.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ json-diff-ts
json-diff-ts - npm
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.. Latest version: 4.9.1, last published: 18 days ago.
      ยป npm install json-diff-ts
    
Published ย  Feb 15, 2026
Version ย  4.9.1
Author ย  Christian Glessner
Find elsewhere
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;
};
๐ŸŒ
GitHub
github.com โ€บ zgrossbart โ€บ jdd
GitHub - zgrossbart/jdd: A semantic JSON compare tool ยท GitHub
JSON Diff expands on the amazing work by the team at jsonlint.com and provides a semantic compare tool for JSON documents. I often work with large documents and it's difficult to see the differences between them.
Author ย  zgrossbart
๐ŸŒ
Quora
quora.com โ€บ How-can-I-compare-two-JSON-objects-with-their-key-value-pairs-using-JavaScript
How to compare two JSON objects with their key-value pairs using JavaScript - Quora
Answer (1 of 6): There are actually a couple of ways to do this, and it depends on the type of data you have. If you've a JSON format set, like a particular API endpoint returning JSON, and you want to compare the same structure for a possible change in values, you can directly convert both payl...
๐ŸŒ
JSON Compare
jsoncompare.org
JSON Compare - Best JSON Diff Tools
It helps to Compare and find proper different in JSON Code, JSON files.
๐ŸŒ
GitHub
gist.github.com โ€บ anastaciocintra โ€บ 6a9bf013d8bd940f857d5c9ad08990e1
Compare two objects JS ยท GitHub
yeap, just change this line: export const isEqual = (obj1: any, obj2: any, checkDataOrder: boolean = false) => { I'm adding another file for using with typescript ...
๐ŸŒ
DEV Community
dev.to โ€บ keploy โ€บ how-to-compare-json-data-a-complete-guide-2f95
How to Compare JSON Data: A Complete Guide - DEV Community
October 21, 2024 - โ€ข JSONCompare: It offers a detailed ... for JSON Comparison โ€ข JavaScript: The lodash library provides a helpful _.isEqual() function for deep comparison....
๐ŸŒ
ExtendsClass
extendsclass.com โ€บ json-diff.html
JSON diff - Online JSON Compare tool
This tool allows to compare two JSON online, and visualize the diff. ... Copy and paste, drag and drop a JSON file or directly type in the editors above, and they will be automatically compared if the two JSON are valids. You can also click on "load JSON from URL" button to load your JSON data ...
๐ŸŒ
@url-decode.com
url-decode.com โ€บ tool โ€บ json-diff
JSON Diff - Best Online JSON Compare Tool
Use this JSON Diff tool to validate, format, and compare two JSON documents. Upload JSON code, compare, and find differences between two JSON objects.
๐ŸŒ
DEV Community
dev.to โ€บ jsonviewertool โ€บ compare-two-json-files-online-json-diff-tool-for-developers-46l9
Compare Two JSON Files Online โ€“ JSON Diff Tool for Developers - DEV Community
5 days ago - Compare Two JSON Files Online โ€“ JSON Diff Tool for Developers If you work with APIs,... Tagged with webdev, javascript, api, json.