Ready
Ln 1, Col 1Blog

Tool Kumar tool

JSON to PHP classes

Generate PHP 8.1 or newer classes with typed readonly properties promoted in the constructor. Each class has a fromArray method that maps the original JSON keys and builds nested objects.

How to use this tool

  1. Paste a representative response, including records whose fields differ.
  2. Review the generated output and change the root name if needed.
  3. Copy or download the code, then check the types against your API contract.

Worked example

This output comes from the built-in converter. The two item prices infer a floating-point field; the null and string notes infer a nullable field.

Input

{
  "user-id": 42,
  "profile": {
    "city": "London",
    "created_at": "2026-10-14T09:00:00Z"
  },
  "items": [
    {
      "price": 12,
      "note": null
    },
    {
      "price": 12.5,
      "note": "gift"
    }
  ]
}

Output

<?php

declare(strict_types=1);

final class Root
{
    public function __construct(
        public readonly int $userId,
        public readonly Profile $profile,
        /** @var Item[] */
        public readonly array $items,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            userId: $data['user-id'],
            profile: Profile::fromArray($data['profile']),
            items: array_map(static fn ($x) => Item::fromArray($x), $data['items']),
        );
    }
}

final class Profile
{
    public function __construct(
        public readonly string $city,
        public readonly string $createdAt,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            city: $data['city'],
            createdAt: $data['created_at'],
        );
    }
}

final class Item
{
    public function __construct(
        public readonly float $price,
        public readonly ?string $note,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            price: $data['price'],
            note: $data['note'],
        );
    }
}

Choose an array or a typed model

For a PHP associative array, json_decode with true is enough. To use the generated models, pass that array to Root::fromArray. JSON_THROW_ON_ERROR reports malformed input.

If an ID exceeds PHP_INT_MAX, JSON_BIGINT_AS_STRING can preserve its digits, but you must change the corresponding generated float property to string. An inferred float is not an exact ID type.

$data = json_decode($responseBody, true, 512, JSON_THROW_ON_ERROR);
$model = Root::fromArray($data);

Options and edge cases

  • The output starts with declare(strict_types=1) and emits final classes. Nested object arrays get PHPDoc item types and array_map construction.
  • Nullable values use ?type; a null-only or mixed field uses mixed. Dates stay string.
  • Integers use int and integers beyond signed 64-bit use float. That can lose precision. Review PHP platform limits and decode large IDs as strings when needed.
  • There is no toArray or JsonSerializable implementation. Add explicit serialization if you need original JSON keys when encoding the classes.

Private by default

Your document stays in this browser. Tool Kumar parses, converts and creates downloads on your device, with no account or upload step.

JSON to PHP classes questions

Can I convert JSON to a PHP associative array?
Yes, using PHP itself: json_decode($json, true, 512, JSON_THROW_ON_ERROR). This generator emits typed model classes rather than a PHP array literal.
Which PHP version does the generated code need?
PHP 8.1 or newer for readonly promoted properties. The output also uses typed properties, named arguments and strict_types.
How do nested objects become classes?
fromArray calls the nested class fromArray method. Arrays of nested objects use array_map, with item types recorded in PHPDoc.
Does it preserve keys when encoding back to JSON?
The fromArray method preserves input mappings. The output has no custom encoder, so json_encode on these objects uses property names. Add JsonSerializable or a toArray method if you need the original wire keys.

Related tools

JSON to Python, JSON to Java, JSON Schema generator.