Appliquer JSON Patch (RFC 6902) operations
JSON Patch defines un sequence de operations (ajouter, supprimer, remplacer, move, copy, tester) en modify un JSON document atomically.
- 1
JSON Patch (RFC 6902) applies an array of operations to a JSON document. Operations: add (insert or replace), remove (delete), replace (overwrite), move (relocate), copy (duplicate), test (assert value before proceeding). Each operation uses a JSON Pointer path.
- 2 Formateur JSON →
Formater both the original document and the patch array to verify they are valid JSON. The patch is an array of {op, path, value?} objects. Format them to catch syntax errors before applying.
- 3 Validateur JSON →
Valider the patch structure: each operation must have "op" (one of add/remove/replace/move/copy/test) and "path" (a JSON Pointer). Add and replace require "value"; move and copy require "from"; test requires "value" for comparison.
- 4
The "test" operation is powerful for conditional patches: it verifies a value before applying subsequent operations. If any test fails, the entire patch is rejected (atomicity). Use test to implement optimistic concurrency: test the version field, then apply changes.
Questions fréquentes
- Is JSON Patch atomic?
- Yes. If any operation fails (e.g., test assertion fails, path does not exist for remove), the entire patch is rejected and the document is unchanged. This makes it safe for concurrent modifications with proper version testing.
- JSON Patch vs JSON Merge Patch?
- JSON Patch (RFC 6902) uses explicit operations (add/remove/replace) — more expressive. JSON Merge Patch (RFC 7396) is simpler: a single JSON object where null means delete and other values mean replace. Use Merge Patch for simple updates, JSON Patch for complex operations.