Deep diff and patch JSON objects. RFC 6902 compliant.
npm install json-diff-patchimport { diff, patch, applyPatch } from 'json-diff-patch';
const original = {
name: 'John',
age: 30,
address: { city: 'NYC' }
};
const modified = {
name: 'John',
age: 31,
address: { city: 'LA' },
email: 'john@example.com'
};
// Generate diff (RFC 6902 format)
const operations = diff(original, modified);
// [
// { op: 'replace', path: '/age', value: 31 },
// { op: 'replace', path: '/address/city', value: 'LA' },
// { op: 'add', path: '/email', value: 'john@example.com' }
// ]
// Apply patch
const result = patch(original, operations);
// Or apply incrementally
let doc = original;
for (const op of operations) {
doc = applyPatch(doc, op);
}- RFC 6902 compliant
- Deep object comparison
- Array diff support
- Immutable operations
- TypeScript-first
- Zero dependencies
MIT