Skip to content
XUtilHub

JSON to PHP Class Generator (PHP 8)

Convert JSON to PHP 8 classes with constructor promotion, readonly typed properties and a fromArray() factory that builds nested objects.

  • Free, no sign-up
  • Runs in your browser, nothing uploaded
Input
Code

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

JSONPHP
"text"string
42int
3.14float
truebool
[...]array + @var list<T>
{...}class with fromArray()
null or missing?T = null
unknownmixed

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.

Need a plain array literal for a config file? Use JSON to PHP Array.

How to use the JSON to PHP Class

  1. Paste your data into the input box, upload a file, or click Sample to try it out.
  2. Adjust the options above the editor if you need to (indentation, delimiter, and so on).
  3. The PHP Class appears instantly in the output panel. Any errors are shown with their line and column.
  4. Click Copy to copy the result, or Download to save it as a file.

Frequently asked questions

Which PHP version is required?

PHP 8.0 for constructor promotion, named arguments and the mixed type. readonly properties need PHP 8.1; untick 'readonly properties' for PHP 8.0.

How do I create objects from JSON?

Decode to an associative array and call fromArray: $order = Order::fromArray(json_decode($json, true, flags: JSON_THROW_ON_ERROR));

Why are arrays typed as 'array' with a docblock?

PHP has no generic array types, so the property is typed array and a @var list<T> docblock tells PHPStan, Psalm and IDEs what it contains.

Is this different from JSON to PHP Array?

Yes. JSON to PHP Array produces an array literal for config files. This tool produces typed classes for application code.

Is my JSON uploaded to a server?

No. All JSON tools run entirely in your browser, so your data never leaves your device.

Is there a size limit?

There is no fixed limit. Files of several megabytes work fine; very large files depend on your device's memory.

Do I need an account?

No. Every JSON tool is free and works without signing up.

Last updated:

Related JSON tools