Download the PHP package alto/json-patch without Composer
On this page you can find all versions of the php package alto/json-patch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alto/json-patch
More information about alto/json-patch
Files in alto/json-patch
Package json-patch
Short Description A PHP JSON-Patch library based on RFC 6902 for generating smart diffs, applying patches, and rebuilding data structures.
License MIT
Homepage https://github.com/phpalto/json-patch
Informations about the package json-patch
ALTO \ JSON Patch
A strict, auditable JSON Patch implementation for PHP 8.3+. This library handles two concerns with precision:
- Apply: A deterministic RFC 6902 engine that replays patches exactly.
- Diff: A smart diff generator that produces stable, readable patches.
Built for systems where change history matters.
- Pure PHP: Tiny surface area, no heavy dependencies.
- Strict Types: Built for PHP 8.3+ with strict typing.
- Deterministic: Error model designed for auditability.
- Smart Diffing: Supports standard list replacement or smart "by-id" list diffing for readable patches.
Installation
Why Alto JSON Patch?
For audit logs: Deterministic apply means you can verify patch integrity. Store the parent hash, the patch, and the result hash. Replaying the patch will always produce the same result.
For readable diffs: Generate clean patches that humans can review. Optional identity-based list diffing produces granular operations instead of replacing entire arrays.
For reliability: Pure PHP with strict types. No magic, no surprises.
Quick Start
Generate Patches
Create patches automatically by diffing two states:
Smart List Diffing
By default, lists are replaced entirely when they differ. For granular control, use identity-based diffing:
This produces readable patches where reviewers can see exactly which items changed.
Utility Methods
Audit Trail Example
Supported Operations
All RFC 6902 operations:
add: Add a value at a pathremove: Remove a value at a pathreplace: Replace a value at a pathmove: Move a value from one path to anothercopy: Copy a value from one path to anothertest: Assert a value matches (useful for conditional patches)
Error Handling
Operations throw JsonPatchException with clear messages:
Advanced Usage
Float Comparison
JsonPatch uses strict equality (===) for values. Be aware that json_decode may treat numbers differently depending on flags.
For example, 1.0 (float) is not strictly equal to 1 (int). Ensure your input documents use consistent types if strict equality is required.
Limitations
applyJson: Empty Object vs Array
When using JsonPatch::applyJson(), the underlying json_decode converts empty JSON objects {} into empty PHP arrays
[].
Since PHP does not distinguish between empty associative arrays (objects) and empty indexed arrays (lists), an input of
{"key": {}} may result in {"key": []} after a round-trip.
If strictly preserving {} vs [] is critical, consider using apply() with pre-decoded structures where you can
control the object mapping (e.g. json_decode($json, false) for stdClass).
API Reference
JsonPatch
| Method | Description |
|---|---|
apply(array $doc, array $patch): array |
Apply a patch to a document |
applyJson(string $docJson, string $patchJson, int $flags = 0): string |
Apply patch to JSON string |
diff(array $from, array $to, ?DiffOptions $opts = null): array |
Generate patch from two states |
get(array $doc, string $path): mixed |
Get value at JSON pointer path |
test(array $doc, string $path, mixed $value): bool |
Test if value matches at path |
validate(array $patch): array |
Validate patch structure, returns errors |
DiffOptions
Configure identity-based list diffing:
License
This project is licensed under the MIT License - see the LICENSE file for details.
All versions of json-patch with dependencies
ext-json Version *