This library provides a JavaScript implementation of the JSON Patch (RFC 6902) and JSON Pointer (RFC 6901) specifications. It allows for applying changes to JSON documents, including operations like add, remove, replace, move, copy, and test. The current stable version is 0.7.0. A key characteristic is that all patch operations are applied *in-place*, directly modifying the input document, which can lead to significant side effects. The package supports usage in browsers, Node.js environments via CommonJS, and AMD modules. Given its version and the age of its last known development activity (last commit over 10 years ago on its GitHub repository), the package is considered unmaintained and has not seen updates for several years, making it unsuitable for new projects requiring active support or modern features. It adheres to the RFC standards but lacks modern JavaScript conveniences or TypeScript support.
npm install json-patchVerified import paths — ran on the pinned version, not inferred.
Demonstrates applying various JSON Patch operations (replace, add, remove, copy, test) to an object and handling potential errors, specifically highlighting the library's in-place modification behavior.
To prevent unintended mutations, always clone your document before applying the patch: `const clonedDocument = JSON.parse(JSON.stringify(originalDocument)); jsonpatch.apply(clonedDocument, patch);`
Update your error handling logic to catch `jsonpatch.PatchTestFailed` instead of checking a boolean return value. Example: `try { jsonpatch.apply(doc, [{op: 'test', path: '/foo', value: 'bar'}]) } catch (e) { if (e instanceof jsonpatch.PatchTestFailed) { /* handle failure */ } }`It is strongly recommended to migrate to actively maintained JSON Patch libraries such as `fast-json-patch` or `json-patch-es6` for better support, performance, and modern JavaScript features.
Manually declare types in a `.d.ts` file (e.g., `declare module 'json-patch' { ... }`) to provide type safety, or consider switching to a modern, actively maintained library with native TypeScript support.Ensure the `value` provided in the `test` operation precisely matches the actual value at the `path` in the document, or adjust the patch logic to reflect the expected state.
Carefully verify that each object in your patch array strictly adheres to the JSON Patch RFC 6902 specification for the respective operation type.
Review the `path` or `from` fields in your patch operations to ensure they are valid JSON Pointers and accurately correspond to the structure of your target document.
For CommonJS, use `const jsonpatch = require('json-patch');`. For ESM, use `import jsonpatch from 'json-patch';`. Always access functions via the `jsonpatch` object: `jsonpatch.apply(...)`.No dependency data recorded yet.