Installations
npm install json-diff
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
*
Node Version
16.17.0
NPM Version
8.15.0
Score
98.9
Supply Chain
98.6
Quality
77.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
CoffeeScript (66.56%)
JavaScript (33.44%)
Developer
andreyvit
Download Statistics
Total Downloads
130,926,366
Last Day
95,731
Last Week
420,340
Last Month
1,664,470
Last Year
23,701,648
GitHub Statistics
1,154 Stars
146 Commits
135 Forks
18 Watching
3 Branches
32 Contributors
Bundle Size
25.72 kB
Minified
10.29 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.6
Package Id
json-diff@1.0.6
Unpacked Size
38.70 kB
Size
10.12 kB
File Count
11
NPM Version
8.15.0
Node Version
16.17.0
Publised On
15 May 2023
Total Downloads
Cumulative downloads
Total Downloads
130,926,366
Last day
0.6%
95,731
Compared to previous day
Last week
-4.4%
420,340
Compared to previous week
Last month
11.4%
1,664,470
Compared to previous month
Last year
17.7%
23,701,648
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Dev Dependencies
5
JSON structural diff
Does exactly what you think it does:
Installation
1 npm install -g json-diff
Contribution policy
-
This project is maintained thanks to your contributions! Please send pull requests.
-
I will merge any pull request that adds something useful, does not break existing things, has reasonable code quality and provides/updates tests where appropriate.
-
Anyone who gets a significant pull request merged gets commit access to the repository.
Usage
Simple:
1 json-diff a.json b.json
Detailed:
1 % json-diff --help 2 3 Usage: json-diff [-vCjfonskKp] first.json second.json 4 5 Arguments: 6 <first.json> Old file 7 <second.json> New file 8 9 General options: 10 -v, --verbose Output progress info 11 -C, --[no-]color Colored output 12 -j, --raw-json Display raw JSON encoding of the diff 13 -f, --full Include the equal sections of the document, not just the deltas 14 --max-elisions COUNT Max number of ...s to show in a row in "deltas" mode (before 15 collapsing them) 16 17 -o, --output-keys KEYS Always print this comma separated keys, with their value, if they are 18 part of an object with any diff 19 20 -x, --exclude-keys KEYS Exclude these comma separated keys from comparison on both files 21 22 -n, --output-new-only Output only the updated and new key/value pairs (without marking them as 23 such). If you need only the diffs from the old file, just exchange the 24 first and second json. 25 26 -s, --sort Sort primitive values in arrays before comparing 27 -k, --keys-only Compare only the keys, ignore the differences in values 28 -K, --keep-unchanged-values Instead of omitting values that are equal, output them as they are 29 -p, --precision DECIMALS Round all floating point numbers to this number of decimal places prior 30 to comparison 31 32 -h, --help Display this usage information
In javascript (ES5):
1var jsonDiff = require('json-diff'); 2 3console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' })); 4// Output: 5// { 6// - foo: "bar" 7// + foo: "baz" 8// } 9 10// As above, but without console colors 11console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' }, { color: false })); 12 13// Raw output: 14console.log(jsonDiff.diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 })); 15// Output: 16// { foo: { __old: 'bar', __new: 'baz' } } 17 18// Passing in the "full" option: 19console.log(jsonDiff.diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { full: true })); 20// Output: 21// { foo: { __old: 'bar', __new: 'baz' }, b: 3 }
In javascript (ES6+):
1import { diffString, diff } from 'json-diff'; 2 3console.log(diffString({ foo: 'bar' }, { foo: 'baz' })); 4console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
Features
- colorized, diff-like output
- fuzzy matching of modified array elements (when array elements are object hierarchies)
- "keysOnly" option to compare only the json structure (keys), ignoring the values
- "full" option to output the entire json tree, not just the deltas
- "outputKeys" option to always output the given keys for an object that has differences
- reasonable test coverage (far from 100%, though)
Output Language in Raw-json mode ("full" mode)
ARRAYS
Unless two arrays are equal, all array elements are transformed into 2-tuple arrays:
- The first element is a one character string denoting the equality ('+', '-', '~', ' ')
- The second element is the old (-), new (+), altered sub-object (~), or unchanged (' ') value
1 json-diff.js --full --raw-json <(echo '[1,7,3]') <(echo '[1,2,3]') 2 [ [ " ", 1 ], [ "-", 7 ], [ "+", 2 ], [ " ", 3 ] ]
1 json-diff.js --full --raw-json <(echo '[1,["a","b"],4]') <(echo '[1,["a","c"],4]') 2 [ [ " ", 1 ], [ "~", [ [ " ", "a" ], [ "-", "b" ], [ "+", "c" ] ] ], [ " ", 4 ] ]
- If two arrays are equal, they are left as is.
OBJECTS
Object property values:
- If equal, they are left as is
- Unequal scalar values are replaced by an object containing the old and new value:
1 json-diff.js --full --raw-json <(echo '{"a":4}') <(echo '{"a":5}') 2 { "a": { "__old": 4, "__new": 5 } }
- Unequal arrays and objects are replaced by their diff:
1 json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"a":[4,6]}') 2 { "a": [ [ " ", 4 ], [ "-", 5 ], [ "+", 6 ] ] }
Object property keys:
- Object keys that are deleted or added between two objects are marked as such:
1 json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"b":[4,5]}') 2 { "a__deleted": [ 4, 5 ], "b__added": [ 4, 5 ] } 3 json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"b":[4,6]}') 4 { "a__deleted": [ 4, 5 ], "b__added": [ 4, 6 ] }
Non-full mode
- In regular, delta-only (non-"full") mode, equal properties and values are omitted:
1 json-diff.js --raw-json <(echo '{"a":4, "b":6}') <(echo '{"a":5,"b":6}') 2 { "a": { "__old": 4, "__new": 5 } }
- Equal array elements are represented by a one-tuple containing only a space " ":
1 json-diff.js --raw-json <(echo '[1,7,3]') <(echo '[1,2,3]') 2 [ [ " " ], [ "-", 7 ], [ "+", 2 ], [ " " ] ]
Tests
Run:
1 npm test
Output:
Open to View Test Output 🔽
json-diff@0.5.3 test
coffee -c test; mocha test/*.js
colorizeToArray
✔ should return '
colorize ✔ should return a string with ANSI escapes ✔ should return a string without ANSI escapes on { color: false }
diff
with simple scalar values
✔ should return undefined for two identical numbers
✔ should return undefined for two identical strings
✔ should return { __old:
diff({sort: true}) with arrays ✔ should return undefined for two arrays with the same contents in different order
diff({keepUnchangedValues: true}) with nested object ✔ should return partial object with modified and unmodified elements in the edited scope
diff({full: true})
with simple scalar values
✔ should return the number for two identical numbers
✔ should return the string for two identical strings
✔ should return { __old:
diff({ outputKeys: foo,bar } ✔ should return keys foo and bar although they have no changes ✔ should return keys foo (with addition) and bar (with no changes) ✔ should return keys foo and bar (with addition) ✔ should return nothing as the entire object is equal, no matter that show keys has some of them ✔ should return the keys of an entire object although it has no changes
diff({keysOnly: true})
with simple scalar values
✔ should return undefined for two identical numbers
✔ should return undefined for two identical strings
✔ should return undefined object for two different numbers
with objects
✔ should return undefined for two empty objects
✔ should return undefined for two objects with identical contents
✔ should return undefined for two object hierarchies with identical contents
✔ should return {
diffString ✔ should produce the expected result for the example JSON files ✔ should produce the expected result for the example JSON files with precision set to 1 ✔ should produce the expected colored result for the example JSON files ✔ return an empty string when no diff found
diff({ outputNewOnly: true } ✔ should return only new diffs (added) ✔ should return only new diffs (changed) ✔ should return only new diffs (deleted) ✔ should return only old diffs - exchanged first and second json (added) ✔ should return only old diffs - exchanged first and second json (changed) ✔ should return only old diffs - exchanged first and second json (deleted)
107 passing (74ms)
Change Log
- 1.0.6 Comment out another debugging output.
- 1.0.5 Comment out debugging output(!)
- 1.0.4 Fix typo that broke -o/--output-keys
- 1.0.3 Change from cli-color to colors to reduce package size.
- 1.0.2 Add colorize and colorizeToCallback to module exports (Fix bug #103)
- 1.0.1 Bug fixes: Properly compare date objects; properly exclude keys with -x; improve README readability.
- 1.0.0 Properly distinguish list elements with identical strings of different types e.g.
["true"]
vs[true]
,["0"]
vs[0]
(enabled by switching to a new difflib) - 0.10.0 Add --exclude-keys
- 0.9.1 Fix bug #88
- 0.9.0 Add --output-new-only option
- 0.8.0 Add --keep-unchanged-values option
- 0.7.4 Fix bug #76
- 0.7.3 Revert use of ?? operator in 0.7.2 (which caused a breaking change)
- 0.7.2 Add --maxElisions and --precision options.
- 0.7.1 Add --output-keys option.
- 0.7.0 Add --sort option.
- 0.6.3 Fix ticket #68.
- 0.6.2 Provide examples of setting mode from code.
- 0.6.1 Return exit code 0. Update cli-color to the latest version.
- 0.6.0 Convert project code to ES6.
- 0.5.5 Fix bug in scalarize fuzzy compare logic.
- 0.4.0 Add --keys-only feature.
License
© Andrey Tarantsov. Distributed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE-MIT:0
- Info: FSF or OSI recognized license: MIT License: LICENSE-MIT:0
Reason
Found 8/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Reason
18 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-3w5v-p54c-f74x
- Warn: Project is vulnerable to: GHSA-6x77-rpqf-j6mw
- Warn: Project is vulnerable to: GHSA-hwcf-pp87-7x6p
- Warn: Project is vulnerable to: GHSA-phwq-j96m-2c2q
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-c9f4-xj24-8jqx
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
2
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More