01What is JSON to PHP Class?
Passing raw associative arrays around a PHP app works until someone mistypes a key. Then you get a silent null or a warning three layers away. This tool turns a JSON sample into typed PHP classes, so your editor and static analysers can catch those mistakes early. Each class uses constructor property promotion with public readonly typed properties. Promotion arrived in PHP 8.0, and readonly properties need PHP 8.1, as described in the PHP manual. The file starts with <?php and declare(strict_types=1). If you fill in a namespace, a namespace line follows. Types are inferred from your sample values. Strings become string, whole numbers int, decimals float and booleans bool. A null value becomes mixed, since one null tells us nothing about the real type. Nested objects get their own class named after the key, so address becomes Address. Arrays of objects get an item class such as ItemsItem, typed as array with a /** @var ItemsItem[] */ docblock, because PHP has no typed arrays. Arrays of scalars are typed plain array. Before an item class is built, all array elements are merged. That way the class has every key seen in any element. A key missing or null in some elements becomes nullable, like ?int, and is read with ?? null. Keys in snake_case, kebab-case or with spaces turn into camelCase property names. The original key is kept for array access, so nothing gets lost. A class name PHP reserves, such as List, gets a Data suffix and becomes ListData. Every class also gets two helpers. The static fromArray(array $data) factory builds the object and hydrates nested classes, using array_map for lists. The toArray() method rebuilds the original key structure for serialising back out. A short example. Paste {"user_id":7,"full-name":"Ann"} and you get a Root class with int $userId and string $fullName. Its fromArray reads $data['user_id'] and $data['full-name'], and toArray writes those same keys back. Some limits to keep in mind. Types come from one sample, so a price of 10 is typed int even if the API later sends 10.5. With strict_types on, that mismatch throws a TypeError. Non-nullable fields are read directly, so a missing key raises an undefined array key warning. A top-level array of primitives produces only a comment, with no class. All of the parsing happens in your browser tab, and the JSON you paste is never uploaded.
PHP developers on Laravel, Symfony or plain PHP 8.1+ who consume JSON APIs and want readonly typed models instead of arrays. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to PHP Class
- Paste a representative JSON document into JSON Input, or load one of the examples.
- Set the Class name for the root class. It defaults to Root.
- Optionally type a Namespace such as App\Models to add a namespace line.
- Review the generated classes in PHP Output. Invalid JSON shows a JSON Parse Error message instead.
- Click Copy, then split the classes into separate files to suit PSR-4 autoloading.
03Worked examples
- Input:
{"user_id":7,"full-name":"Ann","meta":null} - Result:
class Root { public function __construct( public readonly int $userId, public readonly string $fullName, public readonly mixed $meta, ) {} public static function fromArray(array $data): static { return new static( userId: $data['user_id'], fullName: $data['full-name'], meta: $data['meta'] ?? null, ); } … - Properties are camelCase, but fromArray still reads the original JSON keys. Trimmed before toArray() and without the <?php header.
- Input:
{"user_id":7,"full-name":"Ann","meta":null} - Result:
public function toArray(): array { return [ 'user_id' => $this->userId, 'full-name' => $this->fullName, 'meta' => $this->meta, ]; } - The toArray() method from the same output writes the original keys back. Excerpt only.
- Input:
{"items":[{"id":1},{"id":2,"qty":3}]} - Result:
/** @var ItemsItem[] */ public readonly array $items, … items: array_map(fn(array $item) => ItemsItem::fromArray($item), $data['items']), … class ItemsItem { public function __construct( public readonly int $id, public readonly ?int $qty, ) {} - The item class goes in a @var docblock. qty is missing from one item, so it becomes ?int. Excerpt only.
- Input:
{"list":1} with Namespace App\Models - Result:
<?php declare(strict_types=1); namespace App\Models; class Root { public function __construct( public readonly int $list, ) {} … - list is reserved in PHP but still valid as a property name, so it is kept. Trimmed after the constructor.
- Input:
{"list":{"a":1},"tags":["x"]} - Result:
public readonly ListData $list, public readonly array $tags, … list: ListData::fromArray($data['list']), tags: $data['tags'], … class ListData { public function __construct( public readonly int $a, ) {} - List cannot be a class name, so the nested class becomes ListData. A string array stays plain array. Excerpt only.
- Input:
[1,2] - Result:
<?php declare(strict_types=1); // Array of primitives — no class structure to generate. - A top-level array of scalars has no shape to model, so only the header and a comment come out.
04Common use cases
- Creating typed DTOs for a third-party API response, such as a payment gateway, in a Laravel or Symfony app
- Replacing loose associative arrays in legacy code with readonly value objects
- Mapping webhook payloads into classes that PHPStan or Psalm can check
- Scaffolding request and response objects for an internal microservice contract
- Building test fixtures where fromArray turns a recorded JSON response into real objects
- Round-tripping data with toArray before sending it back to an API that expects the original snake_case keys
05Tips
- readonly properties need PHP 8.1 or newer. On PHP 8.0 you have to drop the readonly keyword by hand.
- A field that is null in your sample becomes mixed. Paste a sample where it has a real value to get a proper type.
- With strict_types on, a price sampled as 10 is typed int and a later 10.5 throws a TypeError. Change such fields to float yourself.
- fromArray reads non-nullable keys directly, so a missing key raises a warning. Add ?? defaults where the API sometimes omits fields.
- A key named list stays list as a property, because PHP allows reserved words as property and named argument names. Only class names get the Data suffix.
06Related concepts
Data transfer objects, Constructor property promotion, Readonly properties, PSR-4, Type inference.
Frequently Asked Questions
What is JSON to PHP Class?
JSON to PHP Class is a free, browser-based developer tool on AIDToolStack. Convert JSON to PHP classes It requires no installation or sign-up and works entirely in your browser.
Is JSON to PHP Class free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to PHP Class store my data?
No. It runs in your browser; nothing is uploaded.