Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
The json-diff npm package is a tool for comparing JSON objects and generating a diff between them. It is useful for identifying changes between two JSON structures, which can be helpful in various scenarios such as configuration management, data synchronization, and debugging.
Generate Diff
This feature allows you to generate a diff between two JSON objects. The code sample demonstrates how to use the json-diff package to compare two JSON objects and print the differences.
const jsonDiff = require('json-diff');
const oldJson = { name: 'Alice', age: 25 };
const newJson = { name: 'Alice', age: 26 };
const diff = jsonDiff.diffString(oldJson, newJson);
console.log(diff);
Pretty Print Diff
This feature allows you to generate a color-coded diff for better readability. The code sample shows how to enable color in the diff output.
const jsonDiff = require('json-diff');
const oldJson = { name: 'Alice', age: 25 };
const newJson = { name: 'Alice', age: 26 };
const diff = jsonDiff.diffString(oldJson, newJson, { color: true });
console.log(diff);
Compare JSON Files
This feature allows you to compare JSON data from files. The code sample demonstrates how to read JSON data from two files and generate a diff.
const fs = require('fs');
const jsonDiff = require('json-diff');
const oldJson = JSON.parse(fs.readFileSync('old.json', 'utf8'));
const newJson = JSON.parse(fs.readFileSync('new.json', 'utf8'));
const diff = jsonDiff.diffString(oldJson, newJson);
console.log(diff);
The deep-diff package provides a way to find differences between two JavaScript objects. It supports nested structures and can generate detailed diffs. Compared to json-diff, deep-diff offers more granular control over the diffing process and can be used for more complex data structures.
The diff package is a general-purpose text diffing library that can be used to compare JSON strings. While it is not specifically designed for JSON, it can be used to generate diffs for any text data. It offers a variety of diffing algorithms and options for customization.
The jsondiffpatch package is designed specifically for comparing JSON objects and generating patches. It supports complex data structures and can generate both diffs and patches that can be applied to update JSON objects. It offers more advanced features compared to json-diff, such as patch generation and application.
Does exactly what you think it does:
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:
json-diff a.json b.json
Detailed:
% 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). 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' }));
// Output:
// {
// - foo: "bar"
// + foo: "baz"
// }
// As above, but without console colors
console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' }, { color: false }));
// Raw output:
console.log(jsonDiff.diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }));
// Output:
// { foo: { __old: 'bar', __new: 'baz' } }
// Passing in the "full" option:
console.log(jsonDiff.diff({ foo: 'bar', b: 3 }, { foo: 'baz', b: 3 }, { full: true }));
// Output:
// { foo: { __old: 'bar', __new: 'baz' }, b: 3 }
In javascript (ES6+):
import { diffString, diff } from 'json-diff';
console.log(diffString({ foo: 'bar' }, { foo: 'baz' }));
console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
Unless two arrays are equal, all array elements are transformed into 2-tuple arrays:
json-diff.js --full --raw-json <(echo '[1,7,3]') <(echo '[1,2,3]')
[ [ " ", 1 ], [ "-", 7 ], [ "+", 2 ], [ " ", 3 ] ]
json-diff.js --full --raw-json <(echo '[1,["a","b"],4]') <(echo '[1,["a","c"],4]')
[ [ " ", 1 ], [ "~", [ [ " ", "a" ], [ "-", "b" ], [ "+", "c" ] ] ], [ " ", 4 ] ]
Object property values:
json-diff.js --full --raw-json <(echo '{"a":4}') <(echo '{"a":5}')
{ "a": { "__old": 4, "__new": 5 } }
json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"a":[4,6]}')
{ "a": [ [ " ", 4 ], [ "-", 5 ], [ "+", 6 ] ] }
Object property keys:
json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"b":[4,5]}')
{ "a__deleted": [ 4, 5 ], "b__added": [ 4, 5 ] }
json-diff.js --full --raw-json <(echo '{"a":[4,5]}') <(echo '{"b":[4,6]}')
{ "a__deleted": [ 4, 5 ], "b__added": [ 4, 6 ] }
json-diff.js --raw-json <(echo '{"a":4, "b":6}') <(echo '{"a":5,"b":6}')
{ "a": { "__old": 4, "__new": 5 } }
json-diff.js --raw-json <(echo '[1,7,3]') <(echo '[1,2,3]')
[ [ " " ], [ "-", 7 ], [ "+", 2 ], [ " " ] ]
Run:
npm test
Output:
json-diff@0.5.3 test
coffee -c test; mocha test/*.js
colorizeToArray ✔ should return ' ' for a scalar value ✔ should return ' ' for 'null' value ✔ should return ' ' for 'false' value ✔ should return '-', '+' for a scalar diff ✔ should return '-', '+' for 'null' and 'false' diff ✔ should return '-: ' for an object diff with a removed key ✔ should return '+: ' for an object diff with an added key ✔ should return '+: ' for an object diff with an added key with 'null' value ✔ should return '+: ' for an object diff with an added key with 'false' value ✔ should return '+: ' for an object diff with an added key and a non-scalar value ✔ should return ' : ' for an object diff with a modified key ✔ should return '+' for an array diff ✔ should return '-' for an array diff ✔ should handle an array diff with subobject diff ✔ should collapse long sequences of identical subobjects into one '...'
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: , __new: } 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 { __deleted: } when the second object is missing a key ✔ should return { __added: } when the first object is missing a key ✔ should return { : { __old: , __new: } } for two objects with different scalar values for a key ✔ should return { : } with a recursive diff for two objects with different values for a key with arrays of scalars ✔ should return undefined for two arrays with identical contents ✔ should return [..., ['-', ], ...] for two arrays when the second array is missing a value ✔ should return [..., ['+', ], ...] for two arrays when the second one has an extra value ✔ should return [..., ['+', ]] for two arrays when the second one has an extra value at the end (edge case test) with arrays of objects ✔ should return undefined for two arrays with identical contents ✔ should return undefined for two arrays with identical, empty object contents ✔ should return undefined for two arrays with identical, empty array contents ✔ should return undefined for two arrays with identical array contents including 'null' ✔ should return undefined for two arrays with identical, repeated contents ✔ should return [..., ['-', ], ...] for two arrays when the second array is missing a value ✔ should return [..., ['+', ], ...] for two arrays when the second array has an extra value ✔ should return [['+', ], ..., ['+', ]] for two arrays containing objects of 3 or more properties when the second array has extra values (fixes issue #57) ✔ should return [..., ['+', ], ...] for two arrays when the second array has a new but nearly identical object added ✔ should return [..., ['~', ], ...] for two arrays when an item has been modified with reported bugs ✔ should handle type mismatch during scalarize ✔ should handle mixed scalars and non-scalars in scalarize
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: , __new: } object for two different numbers with objects ✔ should return an empty object for two empty objects ✔ should return the object for two objects with identical contents ✔ should return the object for two object hierarchies with identical contents ✔ should return { __deleted: , } when the second object is missing a key ✔ should return { __added: , } when the first object is missing a key ✔ should return { : { __old: , __new: } } for two objects with different scalar values for a key ✔ should return { : , } with a recursive diff for two objects with different values for a key ✔ should return { : , } with a recursive diff for two objects with different values for a key with arrays of scalars ✔ should return an array showing no changes for any element for two arrays with identical contents ✔ should return [[' ', ], ['-', ], [' ', ]] for two arrays when the second array is missing a value ✔ should return [' ', ], ['+', ], [' ', ]] for two arrays when the second one has an extra value ✔ should return [' ', s], ['+', ]] for two arrays when the second one has an extra value at the end (edge case test) with arrays of objects ✔ should return an array of unchanged elements for two arrays with identical contents ✔ should return an array with an unchanged element for two arrays with identical, empty object contents ✔ should return an array with an unchanged element for two arrays with identical, empty array contents ✔ should return an array of unchanged elements for two arrays with identical array contents including 'null' ✔ should return an array of unchanged elements for two arrays with identical, repeated contents ✔ should return [[' ', ], ['-', ], [' ', ]] for two arrays when the second array is missing a value ✔ should return [[' ', ], ['+', ], [' ', ]] for two arrays when the second array has an extra value ✔ should return [[' ', ], ['+', ], [' ', ]] for two arrays when the second array has a new but nearly identical object added ✔ should return [[' ', ], ['~', ], [' ', ]] for two arrays when an item has been modified
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 { __deleted: } when the second object is missing a key ✔ should return { __added: } when the first object is missing a key ✔ should return undefined for two objects with different scalar values for a key ✔ should return undefined with a recursive diff for two objects with different values for a key ✔ should return { : } with a recursive diff when second object is missing a key and two objects with different values for a key with arrays of scalars ✔ should return undefined for two arrays with identical contents ✔ should return undefined for two arrays with when an item has been modified ✔ should return [..., ['-', ], ...] for two arrays when the second array is missing a value ✔ should return [..., ['+', ], ...] for two arrays when the second one has an extra value ✔ should return [..., ['+', ]] for two arrays when the second one has an extra value at the end (edge case test) with arrays of objects ✔ should return undefined for two arrays with identical contents ✔ should return undefined for two arrays with identical, empty object contents ✔ should return undefined for two arrays with identical, empty array contents ✔ should return undefined for two arrays with identical, repeated contents ✔ should return [..., ['-', ], ...] for two arrays when the second array is missing a value ✔ should return [..., ['+', ], ...] for two arrays when the second array has an extra value ✔ should return [..., ['~', ], ...] for two arrays when an item has been modified
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.
FAQs
JSON diff
The npm package json-diff receives a total of 379,959 weekly downloads. As such, json-diff popularity was classified as popular.
We found that json-diff demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.