Gathering detailed insights and metrics for jsondiffpatch
Gathering detailed insights and metrics for jsondiffpatch
Gathering detailed insights and metrics for jsondiffpatch
Gathering detailed insights and metrics for jsondiffpatch
npm install jsondiffpatch
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.7
Supply Chain
99
Quality
79.2
Maintenance
100
Vulnerability
100
License
TypeScript (94.65%)
CSS (2.94%)
JavaScript (2.41%)
Total Downloads
118,689,129
Last Day
104,197
Last Week
1,957,418
Last Month
8,102,400
Last Year
65,462,050
MIT License
5,163 Stars
485 Commits
484 Forks
86 Watchers
3 Branches
39 Contributors
Updated on Aug 31, 2025
Latest Version
0.7.3
Package Id
jsondiffpatch@0.7.3
Unpacked Size
155.49 kB
Size
33.70 kB
File Count
57
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 31, 2025
Cumulative downloads
Total Downloads
Last Day
-0.5%
104,197
Compared to previous day
Last Week
-1.5%
1,957,418
Compared to previous week
Last Month
-1.3%
8,102,400
Compared to previous month
Last Year
252%
65,462,050
Compared to previous year
1
5
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:
1npx jsondiffpatch --help
or as a library:
1// sample data 2const country = { 3 name: 'Argentina', 4 capital: 'Buenos Aires', 5 independence: new Date(1816, 6, 9), 6}; 7 8// clone country, using dateReviver for Date objects 9const country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver); 10 11// make some changes 12country2.name = 'Republica Argentina'; 13country2.population = 41324992; 14delete country2.capital; 15 16const delta = jsondiffpatch.diff(country, country2); 17 18assertSame(delta, { 19 name: ['Argentina', 'Republica Argentina'], // old value, new value 20 population: ['41324992'], // new value 21 capital: ['Buenos Aires', 0, 0], // deleted 22}); 23 24// patch original 25jsondiffpatch.patch(country, delta); 26 27// reverse diff 28const reverseDelta = jsondiffpatch.reverse(delta); 29// also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta); 30 31const delta2 = jsondiffpatch.diff(country, country2); 32assert(delta2 === undefined); 33// undefined => no difference
Array diffing:
1// sample data 2const country = { 3 name: 'Argentina', 4 cities: [ 5 { 6 name: 'Buenos Aires', 7 population: 13028000, 8 }, 9 { 10 name: 'Cordoba', 11 population: 1430023, 12 }, 13 { 14 name: 'Rosario', 15 population: 1136286, 16 }, 17 { 18 name: 'Mendoza', 19 population: 901126, 20 }, 21 { 22 name: 'San Miguel de Tucuman', 23 population: 800000, 24 }, 25 ], 26}; 27 28// clone country 29const country2 = JSON.parse(JSON.stringify(country)); 30 31// delete Cordoba 32country.cities.splice(1, 1); 33 34// add La Plata 35country.cities.splice(4, 0, { 36 name: 'La Plata', 37}); 38 39// modify Rosario, and move it 40const rosario = country.cities.splice(1, 1)[0]; 41rosario.population += 1234; 42country.cities.push(rosario); 43 44// create a configured instance, match objects by name 45const diffpatcher = jsondiffpatch.create({ 46 objectHash: function (obj) { 47 return obj.name; 48 }, 49}); 50 51const delta = diffpatcher.diff(country, country2); 52 53assertSame(delta, { 54 cities: { 55 _t: 'a', // indicates this node is an array (not an object) 56 1: [ 57 // inserted at index 1 58 { 59 name: 'Cordoba', 60 population: 1430023, 61 }, 62 ], 63 2: { 64 // population modified at index 2 (Rosario) 65 population: [1137520, 1136286], 66 }, 67 _3: [ 68 // removed from index 3 69 { 70 name: 'La Plata', 71 }, 72 0, 73 0, 74 ], 75 _4: [ 76 // move from index 4 to index 2 77 '', 78 2, 79 3, 80 ], 81 }, 82});
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
1npm install jsondiffpatch
1import {* as jsondiffpatch} from 'jsondiffpatch'; 2const jsondiffpatchInstance = jsondiffpatch.create(options);
In a browser, you can load a bundle using a tool like esm.sh or Skypack.
1import * as jsondiffpatch from 'jsondiffpatch'; 2 3// Only import if you want text diffs using diff-match-patch 4import { diff_match_patch } from '@dmsnell/diff-match-patch'; 5 6const jsondiffpatchInstance = jsondiffpatch.create({ 7 // used to match objects when diffing arrays, by default only === operator is used 8 objectHash: function (obj) { 9 // this function is used only to when objects are not equal by ref 10 return obj._id || obj.id; 11 }, 12 arrays: { 13 // default true, detect items moved inside the array (otherwise they will be registered as remove+add) 14 detectMove: true, 15 // default false, the value of items moved is not included in deltas 16 includeValueOnMove: false, 17 }, 18 textDiff: { 19 // If using text diffs, it's required to pass in the diff-match-patch library in through this proprty. 20 // Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options. 21 diffMatchPatch: diff_match_patch, 22 // default 60, minimum string length (left and right sides) to use text diff algorithm: google-diff-match-patch 23 minLength: 60, 24 }, 25 propertyFilter: function (name, context) { 26 /* 27 this optional function can be specified to ignore object properties (eg. volatile data) 28 name: property name, present in either context.left or context.right objects 29 context: the diff context (has context.left and context.right objects) 30 */ 31 return name.slice(0, 1) !== '$'; 32 }, 33 cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned 34 (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. 35 instead of true, a function can be specified here to provide a custom clone(value). 36 */ 37 omitRemovedValues: false /* if you don't need to unpatch (reverse deltas), 38 "old"/"left" values (removed or replaced) are not included in the delta. 39 you can set this to true to get more compact deltas. 40 */, 41});
1<!doctype html> 2<html> 3 <head> 4 <link rel="stylesheet" href="./style.css" type="text/css" /> 5 <link 6 rel="stylesheet" 7 href="https://esm.sh/jsondiffpatch@0.6.0/lib/formatters/styles/html.css" 8 type="text/css" 9 /> 10 <link 11 rel="stylesheet" 12 href="https://esm.sh/jsondiffpatch@0.6.0/lib/formatters/styles/annotated.css" 13 type="text/css" 14 /> 15 </head> 16 <body> 17 <div id="visual"></div> 18 <hr /> 19 <div id="annotated"></div> 20 <script type="module"> 21 import * as jsondiffpatch from 'https://esm.sh/jsondiffpatch@0.6.0'; 22 import * as annotatedFormatter from 'https://esm.sh/jsondiffpatch@0.6.0/formatters/annotated'; 23 import * as htmlFormatter from 'https://esm.sh/jsondiffpatch@0.6.0/formatters/html'; 24 25 const left = { a: 3, b: 4 }; 26 const right = { a: 5, c: 9 }; 27 const delta = jsondiffpatch.diff(left, right); 28 29 // beautiful html diff 30 document.getElementById('visual').innerHTML = htmlFormatter.format( 31 delta, 32 left, 33 ); 34 35 // self-explained json 36 document.getElementById('annotated').innerHTML = 37 annotatedFormatter.format(delta, left); 38 </script> 39 </body> 40</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.
No vulnerabilities found.