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

  1. Paste JSON, or load a .json file (up to 5 MB). The root must be an object or an array of objects.
  2. Optionally change the root class name (default Root). It must start with a letter or underscore and contain only letters, digits and underscores.
  3. The PHP code updates as you type. Copy it, or Download it as a .php file. 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 public properties. The file starts with declare(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_name stays $user_name. Otherwise it is renamed in camelCase (first-name becomes $firstName, 2fa becomes $_2fa) and the docblock records the original key.
  • Values of several types get a union such as int|string, or int|string|null when 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 fromArray or jsonSerialize methods, getters and setters, readonly properties, enums, namespaces or date objects: date strings stay string.

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.