jsondecode.com logo

HomeChevronBlogChevronJSON Merge Patch and JSON Patch: Update APIs Without Full Replace

Blog post

JSON Merge Patch and JSON Patch: Update APIs Without Full Replace

Learn JSON Merge Patch (RFC 7396) and JSON Patch (RFC 6902) for partial updates in REST APIs — with JavaScript and Python examples.

author

Shashank Jain

Author

14/06/20262 minutes 6 seconds read
JSON Merge Patch and JSON Patch: Update APIs Without Full ReplaceJSON Merge Patch and JSON Patch: Update APIs Without Full Replace

Article

The Partial Update Problem

REST APIs often need to update only some fields of a resource. Sending the full object on every update is wasteful and risks overwriting fields the client did not intend to change. Two standards solve this: JSON Merge Patch and JSON Patch.

JSON Merge Patch (RFC 7396)

Send only the fields you want to change. Null means delete the field.

// Original resource
{
  "title": "Hello World",
  "content": "My first post",
  "tags": ["intro", "beginner"],
  "published": false
}

// Merge Patch request body
{
  "published": true,
  "tags": null
}

// Result after merge
{
  "title": "Hello World",
  "content": "My first post",
  "published": true
  // tags deleted because value was null
}

Applying Merge Patch in JavaScript

function applyMergePatch(target, patch) {
  if (typeof patch !== 'object' || patch === null) return patch;

  const result = { ...target };
  for (const [key, value] of Object.entries(patch)) {
    if (value === null) {
      delete result[key];
    } else if (typeof value === 'object' && !Array.isArray(value)) {
      result[key] = applyMergePatch(result[key] || {}, value);
    } else {
      result[key] = value;
    }
  }
  return result;
}

JSON Patch (RFC 6902)

JSON Patch is more powerful — an array of operations:

// JSON Patch operations
[
  { "op": "replace", "path": "/published", "value": true },
  { "op": "add", "path": "/tags/-", "value": "new-tag" },
  { "op": "remove", "path": "/draft" },
  { "op": "move", "path": "/archive/title", "from": "/title" },
  { "op": "copy", "path": "/backup", "from": "/content" },
  { "op": "test", "path": "/version", "value": 3 }
]

Applying JSON Patch in JavaScript

// npm install fast-json-patch
const jsonpatch = require('fast-json-patch');

const document = { name: 'Alice', age: 30 };
const patch = [
  { op: 'replace', path: '/age', value: 31 },
  { op: 'add', path: '/email', value: 'alice@example.com' }
];

const result = jsonpatch.applyPatch(document, patch).newDocument;
console.log(result);
// { name: 'Alice', age: 31, email: 'alice@example.com' }

Which to Use

FeatureMerge PatchJSON Patch
SimplicityVery simpleMore complex
Array operationsFull replace onlyAdd/remove/move items
Atomic operationsNoYes (test operation)
Null deletionYes (null = delete)Explicit remove op
Content-Typeapplication/merge-patch+jsonapplication/json-patch+json

FAQ

When should I use PATCH vs PUT?

Use PUT to replace the entire resource. Use PATCH for partial updates. PATCH with JSON Merge Patch or JSON Patch is safer for concurrent edits — you only touch the fields you intend to change.

How do I delete a field with JSON Merge Patch?

Set the field to null in the patch body. The server should interpret null as a delete instruction. Note: this means you cannot use Merge Patch to set a field to null — use JSON Patch's remove operation for that.

Is JSON Patch atomic?

Servers should apply all operations or none (rollback on failure). The test operation lets you add preconditions. Check your server library's documentation for atomicity guarantees.

What is the Content-Type for JSON Patch?

Use application/json-patch+json for JSON Patch and application/merge-patch+json for JSON Merge Patch. Some APIs accept application/json for both.

Can I generate a JSON Patch from two objects?

Yes. Libraries like fast-json-patch have a generate function that diffs two objects and produces the patch operations.

Keep reading

Recent blogs

View all

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.