About the JSON to PHP Class
Generate modern PHP 8 classes from JSON, with promoted, typed and readonly constructor properties and a static fromArray() factory that hydrates nested objects from json_decode output.
Key features
- PHP 8 classes with constructor property promotion
- Typed, optionally readonly properties (PHP 8.1+)
- Static fromArray() factory that hydrates nested objects
- @var list<T> docblocks for typed arrays
- Nullable and missing values default to null
- strict_types enabled
Typed PHP classes from JSON
Working with json_decode arrays means string keys everywhere and no autocomplete. Modern PHP can do much better: typed, readonly value objects. Paste JSON and get classes with constructor promotion and a fromArray() factory that builds nested objects for you.
Example
{ "id": 1001, "price": 9.5, "tags": ["gift"], "customer": { "name": "Ada" }, "coupon": null }
final class Order
{
public function __construct(
public readonly int $id,
public readonly float $price,
/** @var list<string> */
public readonly array $tags,
public readonly Customer $customer,
public readonly mixed $coupon = null,
) {}
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
price: $data['price'],
tags: $data['tags'],
customer: Customer::fromArray($data['customer']),
coupon: ($data['coupon'] ?? null),
);
}
}
Type mapping
| JSON | PHP |
|---|---|
"text" | string |
42 | int |
3.14 | float |
true | bool |
[...] | array + @var list<T> |
{...} | class with fromArray() |
| null or missing | ?T = null |
| unknown | mixed |
Works great with
- Laravel: return them from services, or map HTTP client responses:
Order::fromArray($response->json()). - Symfony: as DTOs alongside the Serializer component.
- Static analysis: PHPStan and Psalm understand the types and docblocks.
Related tools
Need a plain array literal for a config file? Use JSON to PHP Array.
How to use the JSON to PHP Class
- Paste your data into the input box, upload a file, or click Sample to try it out.
- Adjust the options above the editor if you need to (indentation, delimiter, and so on).
- The PHP Class appears instantly in the output panel. Any errors are shown with their line and column.
- Click Copy to copy the result, or Download to save it as a file.