Core JSON Patch functionality for applying patch operations to JSON documents following RFC-6902. These functions handle all standard patch operations: add, remove, replace, move, copy, and test.
Applies a complete JSON Patch array to a document, processing all operations in sequence.
/**
* Apply a full JSON Patch array on a JSON document.
* Returns the {newDocument, result} of the patch.
* @param document The document to patch
* @param patch The patch array to apply
* @param validateOperation false for no validation, true for default validation, or custom validator function
* @param mutateDocument Whether to mutate the original document or clone it before applying
* @param banPrototypeModifications Whether to ban modifications to __proto__, defaults to true
* @returns Array of operation results with newDocument property
*/
function applyPatch<T>(
document: T,
patch: ReadonlyArray<Operation>,
validateOperation?: boolean | Validator<T>,
mutateDocument?: boolean,
banPrototypeModifications?: boolean
): PatchResult<T>;Usage Examples:
import { applyPatch } from "fast-json-patch";
const document = { name: "John", age: 30, hobbies: ["reading"] };
const patch = [
{ op: "replace", path: "/name", value: "Jane" },
{ op: "add", path: "/email", value: "jane@example.com" },
{ op: "add", path: "/hobbies/1", value: "swimming" }
];
// Apply with validation
const result = applyPatch(document, patch, true);
console.log(result.newDocument);
// { name: "Jane", age: 30, hobbies: ["reading", "swimming"], email: "jane@example.com" }
// Apply without mutating original
const result2 = applyPatch(document, patch, false, false);
// document remains unchanged, result2.newDocument contains the updated versionApplies a single JSON Patch operation to a document.
/**
* Apply a single JSON Patch Operation on a JSON document.
* Returns the {newDocument, result} of the operation.
* @param document The document to patch
* @param operation The operation to apply
* @param validateOperation false for no validation, true for default validation, or custom validator function
* @param mutateDocument Whether to mutate the original document or clone it before applying
* @param banPrototypeModifications Whether to ban modifications to __proto__, defaults to true
* @param index Operation index for error reporting
* @returns Operation result with newDocument, and optionally removed value or test result
*/
function applyOperation<T>(
document: T,
operation: Operation,
validateOperation?: boolean | Validator<T>,
mutateDocument?: boolean,
banPrototypeModifications?: boolean,
index?: number
): OperationResult<T>;Usage Examples:
import { applyOperation } from "fast-json-patch";
const document = { users: [{ name: "Alice" }] };
// Add operation
const addResult = applyOperation(document, {
op: "add",
path: "/users/1",
value: { name: "Bob" }
});
// Remove operation
const removeResult = applyOperation(document, {
op: "remove",
path: "/users/0"
});
console.log(removeResult.removed); // { name: "Alice" }
// Test operation
const testResult = applyOperation(document, {
op: "test",
path: "/users/0/name",
value: "Alice"
});
console.log(testResult.test); // trueApplies a single operation as a reducer function, suitable for use with Array.reduce().
/**
* Apply a single JSON Patch Operation on a JSON document.
* Returns the updated document. Suitable as a reducer.
* @param document The document to patch
* @param operation The operation to apply
* @param index Operation index for error reporting
* @returns The updated document
*/
function applyReducer<T>(document: T, operation: Operation, index: number): T;Usage Examples:
import { applyReducer } from "fast-json-patch";
const document = { counter: 0 };
const operations = [
{ op: "replace", path: "/counter", value: 1 },
{ op: "add", path: "/status", value: "active" }
];
// Use as reducer
const result = operations.reduce(applyReducer, document);
console.log(result); // { counter: 1, status: "active" }Retrieves a value from a JSON document using a JSON pointer path.
/**
* Retrieves a value from a JSON document by a JSON pointer.
* Returns the value.
* @param document The document to get the value from
* @param pointer An escaped JSON pointer
* @returns The retrieved value
*/
function getValueByPointer(document: any, pointer: string): any;Usage Examples:
import { getValueByPointer } from "fast-json-patch";
const document = {
users: [
{ name: "Alice", profile: { age: 25 } },
{ name: "Bob", profile: { age: 30 } }
]
};
// Get nested values
const name = getValueByPointer(document, "/users/0/name"); // "Alice"
const age = getValueByPointer(document, "/users/1/profile/age"); // 30
const users = getValueByPointer(document, "/users"); // entire users array
const root = getValueByPointer(document, ""); // entire documentAll operations inherit from the base operation interface:
interface BaseOperation {
path: string;
}Adds a value at the specified path.
interface AddOperation<T> extends BaseOperation {
op: 'add';
value: T;
}Removes the value at the specified path.
interface RemoveOperation extends BaseOperation {
op: 'remove';
}Replaces the value at the specified path.
interface ReplaceOperation<T> extends BaseOperation {
op: 'replace';
value: T;
}Moves a value from one path to another.
interface MoveOperation extends BaseOperation {
op: 'move';
from: string;
}Copies a value from one path to another.
interface CopyOperation extends BaseOperation {
op: 'copy';
from: string;
}Tests that the value at the specified path equals the given value.
interface TestOperation<T> extends BaseOperation {
op: 'test';
value: T;
}Result of applying a single operation.
interface OperationResult<T> {
/** The removed value (for remove, replace, move operations) */
removed?: any;
/** Test result (for test operations) */
test?: boolean;
/** The updated document */
newDocument: T;
}Result of applying a patch array, extends Array with newDocument property.
interface PatchResult<T> extends Array<OperationResult<T>> {
/** The final updated document after all operations */
newDocument: T;
}Operations can throw JsonPatchError with specific error codes for different failure scenarios:
OPERATION_NOT_AN_OBJECT: Operation is not a valid objectOPERATION_OP_INVALID: Invalid operation typeOPERATION_PATH_INVALID: Invalid path formatOPERATION_VALUE_REQUIRED: Missing required value propertyOPERATION_FROM_REQUIRED: Missing required from propertyOPERATION_PATH_UNRESOLVABLE: Path cannot be resolvedTEST_OPERATION_FAILED: Test operation returned falseSecurity protections prevent modification of __proto__ and constructor.prototype properties by default.