Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
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 [-vjCk] 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
-k, --keys-only Compare only the keys, ignore the differences in values
-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"
// }
console.log(jsonDiff.diff({ foo: 'bar' }, { foo: 'baz' }));
// Output:
// { foo: { __old: 'bar', __new: 'baz' } }
In javascript (ES6+):
import { diffString, diff } from 'json-diff';
console.log(diffString({ foo: 'bar' }, { foo: 'baz' }));
console.log(diff({ foo: 'bar' }, { foo: 'baz' }));
Run:
npm test
Test coverage report:
npm run-script cov
Output:
colorize
✓ should return ' <value>' for a scalar value
✓ should return '-<old value>', '+<new value>' for a scalar diff
✓ should return '-<removed key>: <removed value>' for an object diff with a removed key
✓ should return '+<added key>: <added value>' for an object diff with an added key
✓ should return '+<added key>: <added stringified value>' for an object diff with an added key and a non-scalar value
✓ should return ' <modified key>: <colorized diff>' for an object diff with a modified key
✓ should return '+<inserted item>' for an array diff
✓ should return '-<deleted item>' for an array diff
diff
with simple scalar values
✓ should return undefined for two identical numbers
✓ should return undefined for two identical strings
✓ should return { __old: <old value>, __new: <new value> } object for two different numbers
with objects
✓ should return undefined for two objects with identical contents
✓ should return undefined for two object hierarchies with identical contents
✓ should return { <key>__deleted: <old value> } when the second object is missing a key
✓ should return { <key>__added: <new value> } when the first object is missing a key
✓ should return { <key>: { __old: <old value>, __new: <new value> } } for two objects with diffent scalar values for a key
✓ should return { <key>: <diff> } with a recursive diff for two objects with diffent values for a key
with arrays of scalars
✓ should return undefined for two arrays with identical contents
✓ should return [..., ['-', <removed item>], ...] for two arrays when the second array is missing a value
✓ should return [..., ['+', <added item>], ...] for two arrays when the second one has an extra value
✓ should return [..., ['+', <added item>]] 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 [..., ['-', <removed item>], ...] for two arrays when the second array is missing a value
✓ should return [..., ['+', <added item>], ...] for two arrays when the second array has an extra value
✓ should return [..., ['~', <diff>], ...] for two arrays when an item has been modified (note: involves a crazy heuristic)
✔ 25 tests complete (12ms)
© Andrey Tarantsov. Distributed under the MIT license.
FAQs
JSON diff
The npm package json-diff receives a total of 392,147 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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.