PHPackages                             xp-forge/json-patch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. xp-forge/json-patch

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

xp-forge/json-patch
===================

Implements JSON patch documents described in RFC #6902

v2.1.1(9mo ago)43.5k↓16.7%[1 PRs](https://github.com/xp-forge/json-patch/pulls)BSD-3-ClausePHPPHP &gt;=7.0.0CI passing

Since Jul 5Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/xp-forge/json-patch)[ Packagist](https://packagist.org/packages/xp-forge/json-patch)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-json-patch/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (14)Used By (0)

JSON Patch
==========

[](#json-patch)

[![Build status on GitHub](https://github.com/xp-forge/json-patch/workflows/Tests/badge.svg)](https://github.com/xp-forge/json-patch/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/9296e7a03b273858b32e05552f089a62152f08248483cdf63ebbfc07b6d4d5de/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f6a736f6e2d70617463682f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/json-patch)

Implements JSON patch documents described in [RFC #6902](http://tools.ietf.org/html/rfc6902) and JSON Pointer from [RFC #6901](http://tools.ietf.org/html/rfc6901). Tested against the spec available at . See also .

Example: JSON Patch
-------------------

[](#example-json-patch)

The entry point class is `text.json.patch.Changes`:

```
use text\json\patch\{Changes, TestOperation, AddOperation};

// You can create changes via maps...
$changes= new Changes(
  ['op' => 'test', 'path' => '/best', 'value' => 'Choco Liebniz'],
  ['op' => 'add', 'path' => '/biscuits/1', 'value' => ['name' => 'Ginger Nut']]
);

// ...or by using Operation instances
$changes= new Changes(
  new TestOperation('/best', 'Choco Liebniz'),
  new AddOperation('/biscuits/1', ['name' => 'Ginger Nut'])
);

// If you have a JSON patch document, use the spread operator
$patch= [
  ['op' => 'test', 'path' => '/best', 'value' => 'Choco Liebniz'],
  ['op' => 'add', 'path' => '/biscuits/1', 'value' => ['name' => 'Ginger Nut']]
];
$changes= new Changes(...$patch);
```

Available operations are:

- `AddOperation(string $path, var $value)` - The "add" operation performs one of the following functions, depending upon what the target location references.
- `RemoveOperation(string $path)` - The "remove" operation removes the value at the target location.
- `ReplaceOperation(string $path, var $value)` - The "replace" operation replaces the value at the target location with a new value.
- `MoveOperation(string $from, string $to)` - The "move" operation removes the value at a specified location and adds it to the target location.
- `CopyOperation(string $from, string $to)` - The "copy" operation copies the value at a specified location to the target location.
- `TestOperation(string $path, var $value)` - The "test" operation tests that a value at the target location is equal to a specified value.

To apply the changes, call the `apply()` method and work with the result:

```
$document= [
  'best' => 'Choco Liebniz',
  'biscuits' => [
    ['name' => 'Digestive'],
    ['name' => 'Choco Liebniz']
  ]
];

$changed= $changes->apply($document);

// $changed->successful() := true
// $changed->value() := [
//  'best' => 'Choco Liebniz',
//  'biscuits' => [
//    ['name' => 'Digestive'],
//    ['name' => 'Ginger Nut'],
//    ['name' => 'Choco Liebniz']
//  ]
//];
```

Example: JSON Pointer
---------------------

[](#example-json-pointer)

You can also use the "raw" functionality underneath the `Changes` instance.

```
use text\json\patch\Pointer;

$document= [
  'biscuits' => [
    ['name' => 'Digestive'],
    ['name' => 'Choco Liebniz']
  ]
];

$pointer= new Pointer('/biscuits/1');

// $pointer->exists() := true
// $pointer->value() := ['name' => 'Ginger Nut'];

// This will return an text.json.patch.Applied instance. Use its isError()
// method to discover whether an error occurred.
$result= $pointer->modify('Ginger Nut');

// You can chain calls using the then() method. Closures passed to it will
// only be invoked if applying the operation succeeds, otherwise an Error
// will be returned.
$result= $pointer->remove()->then(function() use($pointer) {
  return $pointer->add('Choco Liebniz');
});
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance58

Moderate activity, may be stable

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~336 days

Recently: every ~488 days

Total

12

Last Release

275d ago

Major Versions

v0.3.0 → v1.0.02016-02-21

v1.1.2 → v2.0.02020-04-10

PHP version history (2 changes)v0.1.0PHP &gt;=5.6.0

v2.0.0PHP &gt;=7.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/07d18d882c8b4aaf3466432f64018214f2771eda333202175431ee7233795376?d=identicon)[thekid](/maintainers/thekid)

---

Top Contributors

[![thekid](https://avatars.githubusercontent.com/u/696742?v=4)](https://github.com/thekid "thekid (101 commits)")

---

Tags

json-patchjson-pointerphprfc-6901rfc-6902xp-frameworkmodulexp

### Embed Badge

![Health badge](/badges/xp-forge-json-patch/health.svg)

```
[![Health](https://phpackages.com/badges/xp-forge-json-patch/health.svg)](https://phpackages.com/packages/xp-forge-json-patch)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
