effect-io-ai

Package: effect
Module: JsonPatch

JsonPatch.apply

Applies a JSON Patch to a JSON document.

When to use

Use to execute patches generated by get, transform documents with manually constructed patches, or process patch operations from external sources.

Details

Executes patch operations sequentially, so later operations see changes made by earlier operations. It never mutates the input document; array and object operations copy the affected containers. An empty patch returns the original reference, and a root replace (path: "") returns the provided value directly.

Gotchas

Invalid paths, missing properties, and out-of-bounds array indices throw errors.

Example (Applying a patch)

import { JsonPatch } from "effect"

const document = { items: [1, 2, 3], total: 6 }
const patch: JsonPatch.JsonPatch = [
  { op: "add", path: "/items/-", value: 4 },
  { op: "replace", path: "/total", value: 10 }
]

const result = JsonPatch.apply(patch, document)
// { items: [1, 2, 3, 4], total: 10 }

See

Signature

declare const apply: (patch: JsonPatch, oldValue: Schema.Json) => Schema.Json

Source

Since v4.0.0