JSON Patch Diff And Apply (RFC 6902)
Generate RFC 6902 add, remove and replace operations from two JSON values, then apply them safely with JSON Pointer escaping.
Code
export type JsonValue =
| null
| boolean
| number
| string
| JsonValue[]
| { [key: string]: JsonValue };
export type JsonPatchOperation =
| { op: "add"; path: string; value: JsonValue }
| { op: "remove"; path: string }
| { op: "replace"; path: string; value: JsonValue };
function isPlainObject(value: unknown): value is Record<string, JsonValue> {
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
function deepEqual(left: unknown, right: unknown): boolean {
if (Object.is(left, right)) return true;
if (Array.isArray(left) && Array.isArray(right)) {
return left.length === right.length &&
left.every((value, index) => deepEqual(value, right[index]));
}
if (isPlainObject(left) && isPlainObject(right)) {
const leftKeys = Object.keys(left);
const rightKeys = Object.keys(right);
return leftKeys.length === rightKeys.length &&
leftKeys.every((key) =>
Object.prototype.hasOwnProperty.call(right, key) && deepEqual(left[key], right[key]));
}
return false;
}
function encodeToken(token: string): string {
return token.replace(/~/g, "~0").replace(/\//g, "~1");
}
function decodeToken(token: string): string {
return token.replace(/~1/g, "/").replace(/~0/g, "~");
}
function joinPath(base: string, key: string): string {
return base + "/" + encodeToken(key);
}
function cloneJson(value: JsonValue): JsonValue {
return structuredClone(value);
}
export function diffJson(
before: JsonValue,
after: JsonValue,
basePath: string = "",
): JsonPatchOperation[] {
if (deepEqual(before, after)) return [];
if (isPlainObject(before) && isPlainObject(after)) {
const operations: JsonPatchOperation[] = [];
for (const key of Object.keys(before)) {
if (!Object.prototype.hasOwnProperty.call(after, key)) {
operations.push({ op: "remove", path: joinPath(basePath, key) });
}
}
for (const [key, value] of Object.entries(after)) {
const path = joinPath(basePath, key);
if (!Object.prototype.hasOwnProperty.call(before, key)) {
operations.push({ op: "add", path, value: cloneJson(value) });
} else {
operations.push(...diffJson(before[key], value, path));
}
}
return operations;
}
return [{ op: "replace", path: basePath, value: cloneJson(after) }];
}
function arrayIndex(token: string, length: number, allowEnd: boolean): number {
if (token === "-") {
if (!allowEnd) throw new RangeError("append is only valid for add");
return length;
}
if (!/^(0|[1-9]\d*)$/.test(token)) throw new SyntaxError("invalid array index: " + token);
const index = Number(token);
const maximum = allowEnd ? length : length - 1;
if (index > maximum) throw new RangeError("array index out of range: " + token);
return index;
}
export function applyJsonPatch(
document: JsonValue,
operations: readonly JsonPatchOperation[],
): JsonValue {
let result = cloneJson(document);
for (const operation of operations) {
if (operation.path !== "" && !operation.path.startsWith("/")) {
throw new SyntaxError("invalid JSON Pointer: " + operation.path);
}
const tokens = operation.path === ""
? []
: operation.path.slice(1).split("/").map(decodeToken);
if (tokens.length === 0) {
if (operation.op === "remove") throw new TypeError("cannot remove the document root");
result = cloneJson(operation.value);
continue;
}
let parent: JsonValue = result;
for (let index = 0; index < tokens.length - 1; index += 1) {
const token = tokens[index];
if (token === "__proto__" || token === "constructor" || token === "prototype") {
throw new TypeError("unsafe patch path: " + operation.path);
}
if (Array.isArray(parent)) {
parent = parent[arrayIndex(token, parent.length, false)];
} else if (isPlainObject(parent)) {
if (!Object.prototype.hasOwnProperty.call(parent, token)) {
throw new RangeError("missing patch path: " + operation.path);
}
parent = parent[token];
} else {
throw new RangeError("patch parent is not a container");
}
}
const last = tokens[tokens.length - 1];
if (last === "__proto__" || last === "constructor" || last === "prototype") {
throw new TypeError("unsafe patch path: " + operation.path);
}
if (Array.isArray(parent)) {
const index = arrayIndex(last, parent.length, operation.op === "add");
if (operation.op === "add") parent.splice(index, 0, cloneJson(operation.value));
else if (operation.op === "remove") parent.splice(index, 1);
else parent[index] = cloneJson(operation.value);
} else if (isPlainObject(parent)) {
if (operation.op === "remove") {
if (!Object.prototype.hasOwnProperty.call(parent, last)) {
throw new RangeError("missing patch path: " + operation.path);
}
delete parent[last];
} else {
if (operation.op === "replace" &&
!Object.prototype.hasOwnProperty.call(parent, last)) {
throw new RangeError("replace target does not exist: " + operation.path);
}
parent[last] = cloneJson(operation.value);
}
} else {
throw new RangeError("patch parent is not a container");
}
}
return result;
}Inside the snippet
Count the functions isPlainObject, deepEqual, encodeToken and decodeToken as the entire surface area of JSON Patch Diff And Apply (RFC 6902).
The nesting pairs for (const key of Object.keys(before)) with for (const [key, value] of Object.entries(after)) — the inner runs once per outer step. Instead of silently continuing, the code raises RangeError, SyntaxError and TypeError at the guard.
It builds and returns a fresh value rather than reusing its inputs.
Watch out for
- Uses
document, so it needs a browser context — it will not run as-is in Node without shims. - The guard
!allowEndshort-circuits the flow with an earlythrowwhen its condition holds.
Using it
The main signature reads export function diffJson( before: JsonValue, after: JsonValue, basePath: string = "", )
The surface worth importing is diffJson; the helpers around it are private to the snippet.
Key statements
return structuredClone(value);
if (!/^(0|[1-9]\d*)$/.test(token)) throw new SyntaxError("invalid array index: " + token);
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
Interactive Tools for This Topic
FAQ
- Does `diffJson` modify its input?
- It does not. Inputs are only read; the return value is freshly built.
- How does the snippet fail?
- `RangeError`, `SyntaxError` and `TypeError` is raised at the guard (`!allowEnd`), so invalid state never reaches the main logic.
- Does JSON Patch Diff And Apply (RFC 6902) run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.