NewYour coding agent can read the release notes before it upgrades.Set up the MCP server →
Packagist, #2081 most downloaded on Packagist
Editable JSON AST with visitor traversal and formatting-preserving printing.
Latest stable
1.0.37
Last release 1 months ago
10 Oct 2026
Release timing varies
gaps range from 8 days to 2 months
Nearly every release is documented
notes for 60 of the last 60 stable releases
Nothing withdrawn
no release was ever pulled
4 months old
138 releases · first in 2026
Drag across the ruler to compare, or tap a release to open it.
 ![PHPStan]
deploy job failures by @samsonasik with @Copilot in https://github.com/boundwize/jsonrecast/pull/27Full Changelog: https://github.com/boundwize/jsonrecast/compare/0.2.6...0.2.7
 ![PHPStan]
Your coding agent can read these notes before it upgrades. Set up the MCP server →
One column per month.
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
Full Changelog: https://github.com/boundwize/jsonrecast/compare/0.1.4...0.1.5
 ![PHPStan]
Full Changelog: https://github.com/boundwize/jsonrecast/compare/0.1.3...0.1.4
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
 ![PHPStan]
Full Changelog: https://github.com/boundwize/jsonrecast/compare/0.0.2...0.0.3
Fix inline array/object formatting by @samsonasik in https://github.com/boundwize/jsonrecast/pull/1
Full Changelog: https://github.com/boundwize/jsonrecast/compare/0.0.1...0.0.2
This is an early 0.0.1 release. The API should be considered unstable — breaking changes may occur in minor versions before 1.0.0.
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="docs/assets/jsonrecast-dark-mode.svg"> <source media="(prefers-color-scheme: light)" srcset="docs/assets/jsonrecast-light-mode.svg"> <img alt="JsonRecast Logo" src="docs/assets/jsonrecast-light-mode.svg" width="300"> </picture> </p>
A PHP JSON parser that turns JSON into an editable AST, supports visitor-based traversal, and prints changes back while preserving the original formatting.
Inspired by PHP-Parser, built for tools that need to modify JSON files safely.
JsonRecast is optimized for safely changing files. It keeps the original structure and formatting where possible, so automated tools can modify JSON without creating noisy diffs.
The AST stays clean. Change metadata lives in the traversal result.
composer require boundwize/jsonrecast
NodeJsonTraverserNodeJsonVisitorNodeJsonPath1, 1.0, and 1e0Start by parsing JSON into a document node:
use Boundwize\JsonRecast\JsonRecast;
$jsonContent = '{"name": "jsonrecast", "private": true}';
$document = JsonRecast::parse($jsonContent);
The result is an AST. Dump it when you want to understand the node shape your visitors will receive:
echo JsonRecast::dumpAst($document);
JsonDocument
value: ObjectNode
items:
[0]: ObjectItemNode
key: StringNode(value: "name")
value: StringNode(value: "jsonrecast")
[1]: ObjectItemNode
key: StringNode(value: "private")
value: BooleanNode(value: true)
The structure follows the JSON shape:
JsonDocument wraps the root JSON valueObjectNode and ArrayNode contain item nodesObjectItemNode contains a string key and a value nodeStringNode, NumberNode, BooleanNode, and NullNodeArrays use the same pattern with ArrayItemNode:
$document = JsonRecast::parse('["json", 1, null]');
echo JsonRecast::dumpAst($document);
JsonDocument
value: ArrayNode
items:
[0]: ArrayItemNode
value: StringNode(value: "json")
[1]: ArrayItemNode
value: NumberNode(rawValue: "1")
[2]: ArrayItemNode
value: NullNode
After traversal, the dumper also accepts JsonRecastResult:
$result = JsonRecast::traverse($document, $visitor);
echo JsonRecast::dumpAst($result);
Pass a named option when you need parser metadata:
echo JsonRecast::dumpAst($document, includeAttributes: true);
You can also use the utility directly:
use Boundwize\JsonRecast\AstDumper;
echo (new AstDumper(includeAttributes: true))->dump($document);
Given this JSON:
{
"name": "acme/demo",
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/Fixtures/App"
]
},
"minimum-stability": "dev"
}
You can edit name, add a PSR-4 namespace, and delete stale object or array data in one traversal:
use Boundwize\JsonRecast\JsonRecast;
use Boundwize\JsonRecast\Node\ArrayNode;
use Boundwize\JsonRecast\Node\NodeJson;
use Boundwize\JsonRecast\Node\ObjectItemNode;
use Boundwize\JsonRecast\Node\ObjectNode;
use Boundwize\JsonRecast\Node\StringNode;
use Boundwize\JsonRecast\NodePath\NodeJsonPath;
use Boundwize\JsonRecast\NodeVisitor\NodeJsonVisitor;
use Boundwize\JsonRecast\NodeVisitor\NodeJsonVisitorAbstract;
$document = JsonRecast::parse($json);
$result = JsonRecast::traverse($document, new class extends NodeJsonVisitorAbstract {
public function enterNode(NodeJson $node, NodeJsonPath $path): null|NodeJson|int
{
if (
$node instanceof ObjectItemNode
&& $path->isRoot()
) {
if ($node->key->value === 'name') {
$node->value = new StringNode('boundwize/jsonrecast');
return $node;
}
if ($node->key->value === 'minimum-stability') {
return NodeJsonVisitor::REMOVE_NODE;
}
}
if ($node instanceof ObjectNode && $path->matches(['autoload', 'psr-4'])) {
$node->set('Boundwize\\JsonRecast\\', new StringNode('src/'));
return $node;
}
if ($node instanceof ArrayNode && $path->matches(['autoload-dev', 'classmap'])) {
$removed = false;
foreach ($node->items as $index => $item) {
if (! $item->value instanceof StringNode || $item->value->value !== 'tests/Fixtures/App') {
continue;
}
$node->removeAt($index);
$removed = true;
}
return $removed ? $node : null;
}
return null;
}
});
echo JsonRecast::print($result);
The printed JSON keeps the surrounding formatting and only rewrites the changed pieces:
{
"name": "boundwize/jsonrecast",
"autoload": {
"psr-4": {
"App\\": "app/",
"Boundwize\\JsonRecast\\": "src/"
}
},
"autoload-dev": {
"classmap": [
]
}
}
Use leaveNode() when a parent decision depends on child nodes that may already have changed. For example, after the classmap array item is removed, you can remove the now-empty root autoload-dev item:
// ...
public function leaveNode(NodeJson $node, NodeJsonPath $path): ?int
{
if (
! $node instanceof ObjectItemNode
|| ! $path->isRoot()
|| $node->key->value !== 'autoload-dev'
|| ! $node->value instanceof ObjectNode
) {
return null;
}
$classmapItem = $node->value->get('classmap');
if (
! $classmapItem instanceof ObjectItemNode
|| ! $classmapItem->value instanceof ArrayNode
|| $classmapItem->value->items !== []
) {
return null;
}
return NodeJsonVisitor::REMOVE_NODE;
}
// ...
With that hook added, the printed JSON becomes:
{
"name": "boundwize/jsonrecast",
"autoload": {
"psr-4": {
"App\\": "app/",
"Boundwize\\JsonRecast\\": "src/"
}
}
}
This is an early 0.0.1 release. The API should be considered unstable — breaking changes may occur in minor versions before 1.0.0.
Feedback, issues, and contributions are very welcome at https://github.com/boundwize/jsonrecast