JSON Compare And Patch

JSON Compare and Patch

Written for AI agents. See Log Methodology Note below for details.

Given the work in Serialization Mutable it is clear we need algorithms for computing the JSON Patch and applying the JSON Patch. We need those algorithms to be small (code wise), efficient and such that we can extend with our specifics (immer, jay mutable, list-compare).

A good resource of available work is jsonpatch.

json-joy

Very solid and well written library, yet not really browser ready. License is Apache 2.0. Includes

  • JSON patch apply algorithm
  • great starter code for json-equals which can be a base for comparing two JSON objects for a JSON patch
  • serialization benchmarks in json-pack from which we learn JSON.stringify and JSON.parse are great

jsonpatch.js

old library to just apply a patch on objects.

jsonpatch-js

old library to just apply a patch on objects. can also generate a function that given a patch, generates a function to apply it to objects.

jiff

old library, does both diff and patch. looks like solid and fast implementation. MIT license. It does an O(N^2) array comparison algorithm by first serializing all array elements, then creating a comparison matrix.

JSON-Patch

recently active, MIT. Does diff and patch.

It has a solid compare algorithm at duplex.ts, which does not take into account moved array items.

JSON8

has the patch library, which is a bit old, ISC license. Very efficient and concise diff and apply algorithm, not compliant to JSON-patch, does not take into account moved array items

mutant-json

Only applies patches. 3 years old, MIT license

immutable-json-patch

active. Only applies patches. ISC license.

rfc6902

MIT license, does both diff and patch. has an interesting dynamic programming diff algorithm. However, the algorithm does not do move operations. Instead, it does work to optimize for small insert / remove operations of items, making it a good candidate algorithm.

has also a good apply algorithm which updates in place

Candidate algorithm

NO - simple validation on a 100x100 matrix with 100 updates shows seconds of runtime!!! see /exploration/rfc6902

immer

Immer has a json patch algorithm that is not decoupled from immer, can only be used as part of immer. The algorithm does a comparison between objects that does not handle array added, removed or moved items.


Log Methodology Note

Note: These design logs are written primarily for AI agents as part of the Design Log methodology and made accessible here for human readers. The language and structure are optimized for machine consumption — expect precise, specification-style prose rather than narrative documentation.