Trending

World technology news

→
JSON Patch in 2013: applying ordered changes as a unit
Software Engineering · 2013
Software Engineering2013

JSON Patch in 2013: applying ordered changes as a unit

Use RFC 6902 and JSON Pointer to update a dispatch record, then test stale versions and the consequences of array order.

Archive retrospective · Topic year 2013

Developer's JournalPublished: Nov 27, 20266 min read
Reading progress0%
In this article
  1. Two editors are changing one dispatch record
  2. Read the path and the sequence literally
  3. Work through the sample before running it
  4. Make intermediate edits invisible
  5. Authorize resolved fields and both ends of a move
  6. Regression checks at the boundary

Two editors are changing one dispatch record

A warehouse supervisor reserves dock 2 for a pallet while quality assurance updates handling labels. Both started on revision 14. A form that writes the whole document back can discard changes accepted from another screen moments earlier. RFC 6902, issued in April 2013, defines a shared representation for an ordered list of changes. This retrospective uses a synthetic dispatch record to show how paths, operations, and expected old values interact. The transport format helps us describe small edits without publishing a dedicated endpoint for every field. It still leaves decisions about concurrent writes, authorization, and failure behavior to the service. Start with the business promise. The operator needs to know whether the dock was actually reserved, which is different from accepting a well formed request body.

Technical referencesRFC 6902 JSON Patch ↗

Read the path and the sequence literally

JSON Pointer navigates from the document root through slash-separated tokens. A slash inside a key must be escaped as ~1 and a tilde as ~0. The dock/2 key is a single member, so a path containing separate dock and 2 tokens means something different. Follow the RFC escape order for cases such as ~01. Numeric array tokens refer to positions, starting at zero. JSON Patch gives add a special append use of the final hyphen token. Every operation observes the output of those before it. Removing the first label shifts later positions. A subsequent edit aimed at an original position may now hit another item. Keep fixture cases for middle removals, valid appends, and positions outside the array.

Service architectureConceptual sketch · summary of the article's technical flow

Work through the sample before running it

The first block is the starting record. Only the second block becomes the HTTP body with media type application/json-patch+json. The test expects revision 14. The next steps make the dock held and append inspect to the labels. Increasing revision in this teaching sample makes the outcome explicit. A real service can keep that field server-owned and advance it after the conditional write. Verify that the empty dispatch note survives untouched. If the domain allows held only after open, validate the old state and the completed proposal. Run the same request against revision 15 and expect the opening test to fail. Confirm by reading stored state and checking that no new label or dock change slipped through the rejected attempt.

{
  "revision": 14,
  "labels": ["fragile", "cold"],
  "slots": {"dock/2": {"state": "open"}},
  "dispatchNote": ""
}

[
  {"op": "test", "path": "/revision", "value": 14},
  {"op": "replace", "path": "/slots/dock~12/state", "value": "held"},
  {"op": "add", "path": "/labels/-", "value": "inspect"},
  {"op": "replace", "path": "/revision", "value": 15}
]

Make intermediate edits invisible

Evaluation stops at a failed JSON Patch operation. When the resource is updated through HTTP PATCH, clients must never observe a partially modified representation. Apply the sequence to a working copy, validate the result, and persist it once. Include the expected revision in the write condition to protect the interval between read and commit. The row count tells you whether the compare-and-write worked. A strong ETag and If-Match can express the same precondition at the HTTP boundary. Refetch after a conflict. If the network goes away after a write, read to establish what happened before retrying. An append may be applied twice when the first attempt commits but its response is lost.

Authorize resolved fields and both ends of a move

A valid pointer can still refer to a field the editor cannot modify. Limit editable paths to the subset the role needs. For move and copy, check the source as well as the destination so a private value cannot be transferred into an exposed field. Apply these checks to resolved tokens rather than unparsed string prefixes. Constrain operation count and payload size to what your dispatch UI actually needs. Error responses can identify the failing operation without returning an old private value. Log the record identifier, request identifier, and validated field names where appropriate. A separate audit policy should decide whether any note content may appear in broader logs. Exercise both move and copy after permission changes.

Regression checks at the boundary

Start with a successful edit and check unchanged fields. Move the intentional failure to the second or third operation and require the entire old document to survive. Submit two writes from the same revision and check for one winner regardless of order. Simulate a lost reply followed by a repeat append. A stale revision guard should stop the second append. Run dedicated pointer fixtures for keys with slashes or tildes, missing intermediate objects, and appends at the end. Monitor errors by operation index and outcome category to discover whether a client has drifted from the service contract. The durable lesson is to design the format and the transaction together. The list expresses change, while the service proves what became durable.

Be the first reader ✎ Write a comment

Reader discussion

Discussion prompt

What would you change first for maintainability and testing in your own project?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.