Implementing Partial Updates

1. Using PATCH Method

AspectPATCHPUT
ScopePartial changeFull replacement
BodyChanged fields onlyEntire resource
IdempotentDepends on patch typeYes
Content-Typemerge-patch+json or json-patch+jsonapplication/json

2. Implementing JSON Patch

RFC 6902 — sequence of operations on JSON document.

opDescription
addAdd value to path
removeRemove value at path
replaceReplace value at path
moveMove from one path to another
copyCopy from one path to another
testConditional check (atomic precondition)

Example: JSON Patch

PATCH /users/42
Content-Type: application/json-patch+json

[
  { "op": "replace", "path": "/email", "value": "new@x.com" },
  { "op": "add",     "path": "/tags/-", "value": "premium" },
  { "op": "remove",  "path": "/middleName" }
]

3. Using JSON Merge Patch

RFC 7396 — simpler: send fields to change; null = delete.

Example: JSON Merge Patch

PATCH /users/42
Content-Type: application/merge-patch+json

{
  "email": "new@x.com",
  "middleName": null,        // remove field
  "preferences": {
    "newsletter": false      // merged into existing preferences
  }
}
LimitationWorkaround
Cannot set field to literal nullUse JSON Patch
Cannot patch array elements individuallyUse JSON Patch with index

4. Validating Patch Operations

CheckFailure Code
Path exists (replace/remove)422
Path is patchable (allowlist)403
Value matches schema422
test op succeeds409 (precondition failed)

5. Handling Nested Field Updates

Patch FormatNested Update
JSON Patch{"op":"replace","path":"/address/city","value":"NYC"}
JSON Merge PatchDeep merge of nested objects

6. Implementing Atomic Patch Operations

Warning: JSON Patch is atomic — all operations succeed or none apply. Wrap in DB transaction.
ToolNotes
DB transactionBEGIN ... COMMIT/ROLLBACK
test opPrecondition for atomic conditional update

7. Returning Updated Resource

StatusBody
200 OKFull updated resource
204 No ContentNo body (client must refetch if needed)
200 + Prefer headerPrefer: return=minimalreturn=representation

8. Handling Patch Conflicts

StrategyImplementation
If-Match + ETag412 Precondition Failed on conflict
JSON Patch test409 if test fails
Version fieldCompare in WHERE clause

9. Documenting Patchable Fields

DocumentationContent
Allowed fieldsList patchable paths
Read-only fieldsid, createdAt — return 403 on patch
Side effectsCascading updates, denormalization

10. Using PUT for Full Replacement vs PATCH

Use PUT WhenUse PATCH When
Client has full resourceClient has partial changes
Simple write semanticsBandwidth-sensitive
Resource creation at known URIField-by-field updates
Form submissionsMobile clients, slow networks