You need to pass the options as the last parameter (for .diff there are 3 parameters)

Copyvar jsonDiff = require('json-diff')
console.log(jsonDiff.diff({ foo: 'bar' }, { foo: 'baz' }, {keysOnly: true}));

For diffString there are four params (the 3rd being the colorize options, and the 4th the options)

Copyvar jsonDiff = require('json-diff')
console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' }, undefined, {keysOnly: true}));
Answer from Gabriele Petrioli on Stack Overflow
🌐
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
🌐
npm
npmjs.com › package › jsondiffpatch
jsondiffpatch - npm
JSON diff & patch (object and array diff, text diff, multiple output formats). Latest version: 0.7.3, last published: a year ago. Start using jsondiffpatch in your project by running `npm i jsondiffpatch`. There are 587 other projects in the npm registry using jsondiffpatch.
      » npm install jsondiffpatch
    
Published   Mar 31, 2025
Version   0.7.3
Author   Benjamin Eidelman
🌐
npm
npmjs.com › package › json-diff-kit
json-diff-kit - npm
A better JSON differ & viewer.. Latest version: 1.0.35, last published: 6 days ago. Start using json-diff-kit in your project by running `npm i json-diff-kit`. There are 7 other projects in the npm registry using json-diff-kit.
      » npm install json-diff-kit
    
Published   Mar 03, 2026
Version   1.0.35
Author   Rex Zeng
🌐
GitHub
github.com › benjamine › jsondiffpatch
GitHub - benjamine/jsondiffpatch: Diff & patch JavaScript objects · GitHub
For more example cases (nested objects or arrays, long text diffs) check packages/jsondiffpatch/test/examples/ If you want to understand deltas, see delta format documentation · This works for node, or in browsers if you already do bundling on your app · npm install jsondiffpatch ·
Author   benjamine
🌐
npm
npmjs.com › package › json-schema-diff
json-schema-diff - npm
A language agnostic CLI tool and nodejs api to identify differences between two json schema files.. Latest version: 1.0.0, last published: 3 months ago. Start using json-schema-diff in your project by running `npm i json-schema-diff`. There ...
      » npm install json-schema-diff
    
Published   Nov 21, 2025
Version   1.0.0
Author   Ben Sayers
Top answer
1 of 4
8

Friends! I'm really sorry if I don't understand something, or explain question in wrong terms. All I wanna do is to compare the equality of two pieces of JSON-standardized data like this:

{"skip":0, "limit":7, "arr": [1682, 439, {"x":2, arr:[]}] }

{"skip":0, "limit":7, "arr": [1682, 450, "a", ["something"] }

I'm 100% sure there will be no functions, Date, null or undefined, etc. in these data. I want to say I don't want to compare JavaScript objects in the most general case (with complex prototypes, circular links and all this stuff). The prototype of these data will be Object. I'm also sure lots of skillful programmers have answered this question before me.

The main thing I'm missing is that I don't know how to explain my question correctly. Please feel free to edit my post.

My answer:

First way: Unefficient but reliable. You can modify a generic method like this so it does not waste time looking for functions and undefined. Please note that generic method iterates the objects three times (there are three for .. in loops inside)

Second way: Efficient but has one restriction. I've found JSON.stringify is extremely fast in node.js. The fastest solution that works is:

JSON.stringify(data1) == JSON.stringify(data2)

Very important note! As far as I know, neither JavaScript nor JSON don't matter the order of the object fields. However, if you compare strings made of objects, this will matter much. In my program the object fields are always created in the same order, so the code above works. I didn't search in the V8 documentation, but I think we can rely on the fields creation order. In other case, be aware of using this method.

In my concrete case the second way is 10 times more efficient then the first way.

2 of 4
4

I've done some benchmarking of the various techniques, with underscore coming out on top:

> node nodeCompare.js 

deep comparison res: true took: 418 (underscore)
hash comparison res: true took: 933
assert compare  res: true took: 2025
string compare  res: true took: 1679

Here is the source code:

var _ = require('underscore');

var assert = require('assert');

var crypto = require('crypto');

var large = require('./large.json');

var large2 = JSON.parse(JSON.stringify(large));

var t1 = new Date();
var hash = crypto.createHash('md5').update(JSON.stringify(large)).digest('hex');
var t2 = new Date();

var res = _.isEqual(large, large2);

var t3 = new Date();

var res2 = (hash == crypto.createHash('md5').update(JSON.stringify(large2)).digest('hex'));

var t4 = new Date();

assert.deepEqual(large, large2);

var t5 = new Date();

var res4 = JSON.stringify(large) === JSON.stringify(large2);

var t6 = new Date();

console.log("deep comparison res: "+res+" took: "+ (t3.getTime() - t2.getTime()));
console.log("hash comparison res: "+res2+" took: "+ (t4.getTime() - t3.getTime()));
console.log("assert compare  res: true took: "+ (t5.getTime() - t4.getTime()));
console.log("string compare  res: "+res4+" took: "+ (t6.getTime() - t5.getTime()));
🌐
Jsondiffpatch
jsondiffpatch.com
JsonDiffPatch
JsonDiffPatch is a free online json diff tool and npm library to compare json and get a json diff.
Find elsewhere
🌐
GitHub
github.com › andreyvit › json-diff
GitHub - andreyvit/json-diff: Structural diff for JSON files · GitHub
-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 ... var jsonDiff = require('json-diff'); console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' })); // Output: // { // - foo: "bar" // + foo: "baz" // } // As above, but without console colors console.log(jsonDiff
Author   andreyvit
🌐
npm
npmjs.com › package › json-diff-ts
json-diff-ts - npm
Generates a difference set for JSON objects.
      » npm install json-diff-ts
    
Published   Mar 06, 2026
Version   4.10.0
Author   Christian Glessner
🌐
GitHub
github.com › lukascivil › json-difference
GitHub - lukascivil/json-difference: A simple way to find the difference between two objects or json diff · GitHub
No matter how big your JSON is, the diff will be returned pretty fast. The question you should ask is: Given my old structure what was changed, removed or added to the new structure? This repository contains two npm packages, json-difference and its cli version (json-difference-cli).
Author   lukascivil
🌐
Js
json-diff-kit.js.org
JSON Diff Kit Playground
We cannot provide a description for this page right now
🌐
Skypack
skypack.dev › view › json-diff-kit
npm:json-diff-kit | Skypack
You can install json-diff-kit via various package managers. # using npm npm i json-diff-kit --save # using yarn yarn add json-diff-kit # using pnpm pnpm add json-diff-kit
🌐
npm Trends
npmtrends.com › deep-diff-vs-json-diff-vs-jsondiffpatch
deep-diff vs json-diff vs jsondiffpatch | npm trends
Comparing trends for deep-diff 1.0.2 which has 2,467,440 weekly downloads and 0 GitHub stars vs. json-diff 1.0.6 which has 807,147 weekly downloads and 1,201 GitHub stars vs. jsondiffpatch 0.7.3 which has 2,346,539 weekly downloads and 5,280 GitHub stars.
🌐
npm
npmjs.com › package › json-difference
json-difference - npm
json difference lib. Latest version: 1.16.1, last published: 2 years ago. Start using json-difference in your project by running `npm i json-difference`. There are 13 other projects in the npm registry using json-difference.
      » npm install json-difference
    
Published   Feb 04, 2024
Version   1.16.1
Author   lukascivil
🌐
NPM Compare
npm-compare.com › deep-diff,json-diff,jsondiffpatch
deep-diff vs jsondiffpatch vs json-diff | JavaScript Object Comparison Libraries
Comprehensive comparison of deep-diff, jsondiffpatch, json-diff npm packages, including features, npm download trends, ecosystem, popularity, and performance.
🌐
Cloudsmith
cloudsmith.com › navigator › npm › @types › json-diff
@types/json-diff (1.0.3) - npm Package Quality | Cloudsmith Navigator
$npm install @types/json-diff · /Processing... ✓Done · Start your free trial · 1.0.3 · Stable version · 2years ago · Released · Loading Version Data · NPM on Cloudsmith · Learn more about NPM on Cloudsmith ·
🌐
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 Tools and Libraries ... directly in the terminal. • JSON-diff (npm library): This JavaScript package helps compare JSON objects and outputs differences....
🌐
Npm
npm.io › package › json-diff
Json-diff NPM | npm.io
-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 ... var jsonDiff = require('json-diff'); console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' })); // Output: // { // - foo: "bar" // + foo: "baz" // } // As above, but without console colors console.log(jsonDiff