Gathering detailed insights and metrics for json-diff
Gathering detailed insights and metrics for json-diff
Gathering detailed insights and metrics for json-diff
Gathering detailed insights and metrics for json-diff
npm install json-diff
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,140 Stars
146 Commits
134 Forks
18 Watching
3 Branches
32 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
CoffeeScript (66.56%)
JavaScript (33.44%)
Cumulative downloads
Total Downloads
Last day
-15.1%
80,914
Compared to previous day
Last week
-6.3%
470,336
Compared to previous week
Last month
4.4%
1,990,330
Compared to previous month
Last year
29.6%
24,240,697
Compared to previous year
3
5
Does exactly what you think it does:
1 npm install -g json-diff
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.
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' }));
Unless two arrays are equal, all array elements are transformed into 2-tuple arrays:
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 ] ]
Object property values:
1 json-diff.js --full --raw-json <(echo '{"a":4}') <(echo '{"a":5}') 2 { "a": { "__old": 4, "__new": 5 } }
1 json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"a":[4,6]}') 2 { "a": [ [ " ", 4 ], [ "-", 5 ], [ "+", 6 ] ] }
Object property keys:
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 ] }
1 json-diff.js --raw-json <(echo '{"a":4, "b":6}') <(echo '{"a":5,"b":6}') 2 { "a": { "__old": 4, "__new": 5 } }
1 json-diff.js --raw-json <(echo '[1,7,3]') <(echo '[1,2,3]') 2 [ [ " " ], [ "-", 7 ], [ "+", 2 ], [ " " ] ]
Run:
1 npm 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)
["true"]
vs [true]
, ["0"]
vs [0]
(enabled by switching to a new difflib)© Andrey Tarantsov. Distributed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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