
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
jsondiffpatch
Advanced tools
JSON diff & patch (object and array diff, text diff, multiple output formats)
The jsondiffpatch npm package is a tool for computing the difference between two JSON objects and applying those differences to other JSON objects. It is useful for tasks such as data synchronization, version control, and change tracking in JSON data structures.
Diff
This feature computes the difference between two JSON objects. In this example, the difference between the 'left' and 'right' objects is calculated, resulting in a delta that shows the change in the 'age' property.
const jsondiffpatch = require('jsondiffpatch');
const left = { name: 'John', age: 30 };
const right = { name: 'John', age: 31 };
const delta = jsondiffpatch.diff(left, right);
console.log(delta);
Patch
This feature applies a delta (difference) to a JSON object to produce a new JSON object. In this example, the delta is applied to the 'left' object, resulting in the 'right' object with the updated 'age' property.
const jsondiffpatch = require('jsondiffpatch');
const left = { name: 'John', age: 30 };
const delta = { age: [30, 31] };
const right = jsondiffpatch.patch(left, delta);
console.log(right);
Unpatch
This feature reverts a JSON object to its previous state by applying the inverse of a delta. In this example, the delta is used to revert the 'right' object back to the 'left' object.
const jsondiffpatch = require('jsondiffpatch');
const right = { name: 'John', age: 31 };
const delta = { age: [30, 31] };
const left = jsondiffpatch.unpatch(right, delta);
console.log(left);
Reverse
This feature computes the inverse of a delta, which can be used to revert changes. In this example, the inverse of the delta is calculated, which can then be used to revert the changes made by the original delta.
const jsondiffpatch = require('jsondiffpatch');
const delta = { age: [30, 31] };
const reverseDelta = jsondiffpatch.reverse(delta);
console.log(reverseDelta);
The deep-diff package provides similar functionality for computing differences between JavaScript objects. It offers a more granular approach to detecting changes, including additions, deletions, and modifications. However, it does not provide built-in patching and unpatching capabilities like jsondiffpatch.
The diff package is a general-purpose text and data comparison tool that can be used to compute differences between strings, arrays, and objects. While it is versatile and widely used, it lacks the specialized JSON diffing and patching features provided by jsondiffpatch.
The fast-json-patch package focuses on applying JSON Patch operations as defined by RFC 6902. It is optimized for performance and provides a simple API for applying and validating JSON patches. However, it does not compute differences between JSON objects, which is a key feature of jsondiffpatch.
jsondiffpatch.com
Diff & patch JavaScript objects
objectHash
function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check Array diff documentation./node_modules/.bin/jsondiffpatch left.json right.json
jsondiffpatch.clone(obj)
(deep clone)on your terminal:
npx jsondiffpatch --help
or as a library:
// sample data
const country = {
name: 'Argentina',
capital: 'Buenos Aires',
independence: new Date(1816, 6, 9),
};
// clone country, using dateReviver for Date objects
const country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver);
// make some changes
country2.name = 'Republica Argentina';
country2.population = 41324992;
delete country2.capital;
const delta = jsondiffpatch.diff(country, country2);
assertSame(delta, {
name: ['Argentina', 'Republica Argentina'], // old value, new value
population: ['41324992'], // new value
capital: ['Buenos Aires', 0, 0], // deleted
});
// patch original
jsondiffpatch.patch(country, delta);
// reverse diff
const reverseDelta = jsondiffpatch.reverse(delta);
// also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta);
const delta2 = jsondiffpatch.diff(country, country2);
assert(delta2 === undefined);
// undefined => no difference
Array diffing:
// sample data
const country = {
name: 'Argentina',
cities: [
{
name: 'Buenos Aires',
population: 13028000,
},
{
name: 'Cordoba',
population: 1430023,
},
{
name: 'Rosario',
population: 1136286,
},
{
name: 'Mendoza',
population: 901126,
},
{
name: 'San Miguel de Tucuman',
population: 800000,
},
],
};
// clone country
const country2 = JSON.parse(JSON.stringify(country));
// delete Cordoba
country.cities.splice(1, 1);
// add La Plata
country.cities.splice(4, 0, {
name: 'La Plata',
});
// modify Rosario, and move it
const rosario = country.cities.splice(1, 1)[0];
rosario.population += 1234;
country.cities.push(rosario);
// create a configured instance, match objects by name
const diffpatcher = jsondiffpatch.create({
objectHash: function (obj) {
return obj.name;
},
});
const delta = diffpatcher.diff(country, country2);
assertSame(delta, {
cities: {
_t: 'a', // indicates this node is an array (not an object)
1: [
// inserted at index 1
{
name: 'Cordoba',
population: 1430023,
},
],
2: {
// population modified at index 2 (Rosario)
population: [1137520, 1136286],
},
_3: [
// removed from index 3
{
name: 'La Plata',
},
0,
0,
],
_4: [
// move from index 4 to index 2
'',
2,
3,
],
},
});
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
import {* as jsondiffpatch} from 'jsondiffpatch';
const jsondiffpatchInstance = jsondiffpatch.create(options);
In a browser, you can load a bundle using a tool like esm.sh or Skypack.
import * as jsondiffpatch from 'jsondiffpatch';
// Only import if you want text diffs using diff-match-patch
import { diff_match_patch } from '@dmsnell/diff-match-patch';
const jsondiffpatchInstance = jsondiffpatch.create({
// used to match objects when diffing arrays, by default only === operator is used
objectHash: function (obj) {
// this function is used only to when objects are not equal by ref
return obj._id || obj.id;
},
arrays: {
// default true, detect items moved inside the array (otherwise they will be registered as remove+add)
detectMove: true,
// default false, the value of items moved is not included in deltas
includeValueOnMove: false,
},
textDiff: {
// If using text diffs, it's required to pass in the diff-match-patch library in through this proprty.
// Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options.
diffMatchPatch: diff_match_patch,
// default 60, minimum string length (left and right sides) to use text diff algorithm: google-diff-match-patch
minLength: 60,
},
propertyFilter: function (name, context) {
/*
this optional function can be specified to ignore object properties (eg. volatile data)
name: property name, present in either context.left or context.right objects
context: the diff context (has context.left and context.right objects)
*/
return name.slice(0, 1) !== '$';
},
cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned
(using jsondiffpatch.clone by default), to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching the same objects multiple times without serializing deltas.
instead of true, a function can be specified here to provide a custom clone(value).
*/
omitRemovedValues: false /* if you don't need to unpatch (reverse deltas),
"old"/"left" values (removed or replaced) are not included in the delta.
you can set this to true to get more compact deltas.
*/,
});
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="./style.css" type="text/css" />
<link
rel="stylesheet"
href="https://esm.sh/jsondiffpatch@0.6.0/lib/formatters/styles/html.css"
type="text/css"
/>
<link
rel="stylesheet"
href="https://esm.sh/jsondiffpatch@0.6.0/lib/formatters/styles/annotated.css"
type="text/css"
/>
</head>
<body>
<div id="visual"></div>
<hr />
<div id="annotated"></div>
<script type="module">
import * as jsondiffpatch from 'https://esm.sh/jsondiffpatch@0.6.0';
import * as annotatedFormatter from 'https://esm.sh/jsondiffpatch@0.6.0/formatters/annotated';
import * as htmlFormatter from 'https://esm.sh/jsondiffpatch@0.6.0/formatters/html';
const left = { a: 3, b: 4 };
const right = { a: 5, c: 9 };
const delta = jsondiffpatch.diff(left, right);
// beautiful html diff
document.getElementById('visual').innerHTML = htmlFormatter.format(
delta,
left,
);
// self-explained json
document.getElementById('annotated').innerHTML =
annotatedFormatter.format(delta, left);
</script>
</body>
</html>
To see formatters in action check the Live Demo.
For more details check Formatters documentation
diff()
, patch()
and reverse()
functions are implemented using Pipes & Filters pattern, making it extremely customizable by adding or replacing filters on a pipe.
Check Plugins documentation for details.
FAQs
JSON diff & patch (object and array diff, text diff, multiple output formats)
The npm package jsondiffpatch receives a total of 0 weekly downloads. As such, jsondiffpatch popularity was classified as not popular.
We found that jsondiffpatch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.