JSON to PHP
Generate PHP 8 classes with promoted readonly properties from a JSON sample.
JSON input
Generated code
Enter JSON on the left to generate a type from it.
What is JSON to PHP?
A JSON to PHP converter turns a sample JSON payload into typed PHP class definitions. PHP's json_decode hands back either a stdClass or an associative array, and both are effectively untyped: your IDE can't complete a property name, static analysers like PHPStan and Psalm can't check anything, and a typo in a key surfaces only when the request runs. Modelling the payload as real classes fixes all three. The generated output uses constructor property promotion, which collapses what used to be a property declaration, a constructor parameter, and an assignment into a single line per field, plus readonly so a decoded response can't be mutated by accident somewhere downstream. Because PHP arrays are untyped generics, array properties also carry a docblock naming the element type, which is what static analysers read.
How to use JSON to PHP
- Paste a representative JSON response into the left panel — every promoted property in the generated class comes straight from the keys present in that sample.
- Set a root class name above the output (defaults to "Root"); each nested object becomes its own class named after the key holding it.
- Copy the classes into your project, then hydrate them from json_decode output — either by hand or with a mapper library.
- Use the language picker above the output to emit the same shape in TypeScript, Go, or another supported language.
Examples
Flat object with promoted readonly properties
Input
{"id": 1, "name": "Ada", "active": true}
Output
<?php declare(strict_types=1); final class Root { public function __construct( public readonly int $id, public readonly string $name, public readonly bool $active, ) {} }
Array property carries a docblock for static analysis
Input
{"user": {"id": 1, "name": "Ada", "roles": ["admin"]}}
Output
<?php declare(strict_types=1); final class User { public function __construct( public readonly int $id, public readonly string $name, /** @var string[] */ public readonly array $roles, ) {} } final class Root { public function __construct( public readonly User $user, ) {} }
Common mistakes
- Targeting PHP 7.x with this output — promoted constructor properties and readonly both fail to parse there, producing a syntax error rather than a helpful message.
- Deleting the /** @var string[] */ docblocks as noise — they're the only type information static analysis has about array contents, so removing them silently weakens every check over that data.
- Expecting json_decode to return these classes directly; it returns arrays or stdClass, and mapping into the generated types is a separate step you still have to write or delegate to a library.
Why use this tool
- Uses constructor property promotion, replacing three lines of boilerplate per field with one.
- Marks properties readonly, so a decoded API response can't be quietly mutated later in the request lifecycle.
- Runs entirely client-side, so a response copied from a Laravel or Symfony endpoint never reaches a server just to generate these classes.