Developer Tools

JSON to PHP

Generate modern PHP classes with constructor property promotion, JsonSerializable implementation, and fromArray factories.

  • Runs in your browser
  • Free, no signup
Target Language:
JSON Input 12 lines
PHP Output 16 lines
<?php

declare(strict_types=1);

use JsonSerializable;

class Profile implements JsonSerializable
{
    public function __construct(
        public string $bio,
        public int $reputation,
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            bio: (string) ($data["bio"] ?? ""),
            reputation: (int) ($data["reputation"] ?? 0),
        );
    }

    public function jsonSerialize(): array
    {
        return [
            "bio" => $this->bio,
            "reputation" => $this->reputation,
        ];
    }
}

class Root implements JsonSerializable
{
    public function __construct(
        public int $id,
        public string $name,
        public string $email,
        public bool $isAdmin, // json: is_admin
        /** @var string[] */
        public array $roles,
        public Profile $profile,
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            id: (int) ($data["id"] ?? 0),
            name: (string) ($data["name"] ?? ""),
            email: (string) ($data["email"] ?? ""),
            isAdmin: (bool) ($data["is_admin"] ?? false),
            roles: (array) ($data["roles"] ?? []),
            profile: isset($data["profile"]) && is_array($data["profile"]) ? Profile::fromArray($data["profile"]) : null,
        );
    }

    public function jsonSerialize(): array
    {
        return [
            "id" => $this->id,
            "name" => $this->name,
            "email" => $this->email,
            "is_admin" => $this->isAdmin,
            "roles" => $this->roles,
            "profile" => $this->profile?->jsonSerialize(),
        ];
    }
}

Your files are processed locally in your browser and are not uploaded to our server.

Modern PHP 8+ provides powerful type-safety and concise syntax through constructor property promotion. This tool turns any JSON payload into modern, robust PHP Data Transfer Objects (DTOs) and models.

Generated classes implement JsonSerializable for effortless json_encode() serialization and include static fromArray(array $data) factory methods for safe, typed deserialization from database rows or API responses.

How to use this tool

  1. Paste your JSON payload into the input editor or click "Load Sample".
  2. Select your model style: PHP 8.1+ Constructor Promotion or Typed Class Properties.
  3. Optionally enter a namespace (e.g. App\DTO or Models).
  4. Click "Copy Code" or "Download .php" to save the generated classes.

Modern PHP 8.1+ Constructor Promotion

Rather than writing boilerplate private properties, getters, and setters, the generator utilizes PHP 8 constructor promotion: public function __construct(public string $name, public int $id) {}.

This yields concise, readable, and fully type-checked classes compatible with modern frameworks like Laravel, Symfony, and Slim.

Frequently asked questions

Can I serialize these classes back to JSON with json_encode()?

Yes. Every generated class implements JsonSerializable and provides a jsonSerialize() method that maps PHP properties back to their exact original JSON keys.