JSONPatch
A RFC6902 compliant JSON Patch PHP implementation
Install / Use
/learn @gamringer/JSONPatchREADME
JSONPointer
A RFC6902 compliant JSON Patch PHP implementation
#License JSONPatch is licensed under the MIT license.
#Installation
composer require gamringer/php-json-patch
##Tests
composer install
phpunit
#Documentation
##Operations can be constructed and applied independently
<?php
$target = ['1', '2', '3'];
$operation = new \gamringer\JSONPatch\Operation\Test('/foo', 'bar');
$operation->apply($target);
##Operations can also be constructed from a JSON string
<?php
$operation = \gamringer\JSONPatch\Operation\Test::fromDecodedJSON(json_decode('{"path":"/foo","value":"bar"}'));
##A patch can be constructed from a set of operations
<?php
$patch = new \gamringer\JSONPatch\Patch();
$patch->addOperation(new \gamringer\JSONPatch\Operation\Add('/foo', 'bar'));
$patch->addOperation(new \gamringer\JSONPatch\Operation\Test('/foo', 'bar'));
##A patch can also be constructed from a JSON string
<?php
$patch = \gamringer\JSONPatch\Patch::fromJSON('[{"op":"add","path":"/foo","value":"bar"},{"op":"test","path":"/foo","value":"bar"}]');
##A patch can be applied
<?php
$patch = \gamringer\JSONPatch\Patch::fromJSON('[{"op":"add","path":"/foo","value":"bar"},{"op":"test","path":"/foo","value":"bar"}]');
$target = [];
$patch->apply($target);
var_dump($target);
/* Results:
array(1) {
'foo' =>
string(3) "bar"
}
*/
If the patch fails, it gets completely reverted and an exception is thrown.
<?php
$patch = \gamringer\JSONPatch\Patch::fromJSON('[{"op":"add","path":"/foo","value":"bar"},{"op":"test","path":"/foo","value":"baz"}]');
$target = [];
try {
$patch->apply($target);
} catch (\gamringer\JSONPatch\Exception $e) {
var_dump($target);
}
/* Results:
array(0) {}
*/
Related Skills
node-connect
338.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.6kCommit, push, and open a PR




