How to Generate JSON Patch (RFC 6902)
JSON Patch
When to Use JSON Patch
Generate RFC 6902 patch operations when APIs accept partial updates, you need audit trails of document changes, or you want incremental sync instead of sending full JSON bodies.
Real-World Examples
{"op":"replace","path":"/name","value":"Lin"} — change a single field
{"op":"add","path":"/tags/-","value":"new"} — append to an array
Compare before/after config files to produce a minimal patch for deployment scripts
Common Mistakes
- Using JSON Merge Patch (RFC 7396) syntax — this tool outputs RFC 6902 operations
- Wrong path format — paths must be JSON Pointers starting with
/ - Expecting patches on invalid or differently-ordered JSON to match semantic intent
Developer Tips
- Paste original JSON first, modified JSON second
- Libraries:
fast-json-patchfor apply and compare in Node.js - Review
removeoperations carefully — they are destructive
Frequently asked questions
What patch format is produced?
RFC 6902 operations such as add, remove, replace, move, copy, and test.
How are array changes represented?
Array append uses path /arrayName/- with op add. Index changes use numeric path segments like /items/0.
Can I apply the patch programmatically?
Yes. Use an RFC 6902 library to apply the generated operations array to the original document.