JSON to PHP Class
Infer PHP 8 classes with typed, promoted properties from a JSON sample.
About JSON to PHP Class
Generate PHP 8 classes from a JSON sample, with typed constructor-promoted properties, nullable types and array element types in docblocks.
How to use
- Paste JSON, or load a
.jsonfile (up to 5 MB). The root must be an object or an array of objects. - Optionally change the root class name (default
Root). It must start with a letter or underscore and contain only letters, digits and underscores. - The PHP code updates as you type. Copy it, or Download it as a
.phpfile. The output needs PHP 8.0 or later.
What it does
| JSON | PHP |
|---|---|
| string | string |
| integer | int |
| other number | float |
| boolean | bool |
| array | array, with @param list<T> in the docblock |
| nested object | a named class, RootAddress |
| null in some items | ?T |
| key missing from some items | ?T with a default of null |
| mixed types | a union type |
| only null, or the items of an empty array | mixed |
- Each class has one constructor whose parameters are promoted to
publicproperties. The file starts withdeclare(strict_types=1), and nested classes are listed before the class that uses them. - A property keeps the JSON key as its name when PHP allows it, so
user_namestays$user_name. Otherwise it is renamed in camelCase (first-namebecomes$firstName,2fabecomes$_2fa) and the docblock records the original key. - Values of several types get a union such as
int|string, orint|string|nullwhen null also appears. - Parameters with a default are moved to the end of the constructor, because PHP deprecates a required parameter after an optional one.
- It does not generate
fromArrayorjsonSerializemethods, getters and setters,readonlyproperties, enums, namespaces or date objects: date strings staystring.
Example
{ "id": 1, "user_name": "Ada", "tags": ["admin"] }
<?php
declare(strict_types=1);
class Root
{
/**
* @param list<string> $tags
*/
public function __construct(
public int $id,
public string $user_name,
public array $tags,
) {
}
}
Privacy
Inference runs in the page. No request is made and nothing is stored.
Frequently asked questions
- What is constructor property promotion in PHP?
- Since PHP 8.0 a constructor parameter written with a visibility keyword, such as public int $id, declares the property and assigns it in one step. It replaces the separate property declaration and the $this->id = $id line.
- How do I turn json_decode output into a typed object?
- json_decode($json, true) returns nested arrays, not instances of your classes. Pass the values to the constructor yourself, for example new Root(id: $data['id'], user_name: $data['user_name'], tags: $data['tags']), or use a mapping library such as Symfony Serializer or Valinor.
- How do I type an array of objects in PHP?
- The type system of PHP only has array, with no element type. Document the elements in a docblock, such as @param list<Item> $items, which PHPStan, Psalm and PhpStorm read and check.
- What is the difference between ?string and string|null in PHP?
- None in meaning: ?string is shorthand for string|null. The shorthand only works for a single type, so a union with null has to be written out, as in int|string|null.