What is json-merge-patch?
The json-merge-patch npm package allows you to apply RFC 7396 JSON Merge Patch operations to JSON documents. This is useful for updating JSON objects by merging changes rather than replacing entire objects.
What are json-merge-patch's main functionalities?
Merging JSON Objects
This feature allows you to merge two JSON objects. The `apply` method takes a target object and a patch object, and returns a new object that is the result of applying the patch to the target.
const jsonMergePatch = require('json-merge-patch');
const target = { a: 1, b: 2 };
const patch = { b: 3, c: 4 };
const result = jsonMergePatch.apply(target, patch);
console.log(result); // { a: 1, b: 3, c: 4 }
Removing Properties
This feature allows you to remove properties from a JSON object by setting their values to `null` in the patch object. The `apply` method will remove the property from the target object.
const jsonMergePatch = require('json-merge-patch');
const target = { a: 1, b: 2, c: 3 };
const patch = { b: null };
const result = jsonMergePatch.apply(target, patch);
console.log(result); // { a: 1, c: 3 }
Nested Merging
This feature allows you to merge nested JSON objects. The `apply` method will recursively merge nested objects, updating only the specified properties.
const jsonMergePatch = require('json-merge-patch');
const target = { a: { b: 1, c: 2 } };
const patch = { a: { c: 3, d: 4 } };
const result = jsonMergePatch.apply(target, patch);
console.log(result); // { a: { b: 1, c: 3, d: 4 } }
Other packages similar to json-merge-patch
deepmerge
The deepmerge package allows you to merge JavaScript objects recursively, which is similar to json-merge-patch. However, deepmerge does not follow the RFC 7396 standard and offers more flexibility in merging strategies.
lodash.merge
The lodash.merge function from the lodash library provides deep merging of objects. It is more versatile and widely used compared to json-merge-patch, but it does not specifically implement the JSON Merge Patch standard.
json-patch
The json-patch package implements the JSON Patch (RFC 6902) standard, which is more complex and powerful than JSON Merge Patch. It allows for a wider range of operations, including adding, removing, and replacing elements in JSON documents.
JSON Merge Patch

An implementation of the JSON Merge Patch RFC 7396
JSON Merge Patch (RFC 7396) is a standard format that
allows you to update a JSON document by sending the changes rather than the whole document.
JSON Merge Patch plays well with the HTTP PATCH verb (method) and REST style programming.
Install
Install the current version (and save it as a dependency):
npm
$ npm install json-merge-patch --save
Usage
Applying patches:
var source = {
"title": "Goodbye!",
"author" : {
"givenName" : "John",
"familyName" : "Doe"
}
};
var patch = {
"title": 'Hello!',
"author": {
"familyName": null
}
}
var target = jsonmergepatch.apply(source, patch);
Generating patches:
var source = {
"title": "Goodbye!",
"author" : "John Doe"
};
var target = {
"title": "Hello!",
};
var patch = jsonmergepatch.generate(source, target);
API
jsonmergepatch.apply (obj
Object, patch
Object) : Object
Applies patch
on obj
.
jsonmergepatch.generate (source
Object, target
Object) : patch
Object
Generates a patch
Object from source and target Object.
Running tests
make test
License
MIT