JSON to PHP class
The tool writes a final class per object, handy for Laravel, Symfony or any modern PHP project.
Loading the tool…
Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.
How to use it
- Enter json input and root type name.
- The result appears straight away and updates as you edit.
- Copy the result, or copy a link that keeps the same values to share it.
How it is calculated
int, float, string, bool, array and mixed, with a final class per object and typed public properties. A field that can be null or missing becomes ?T defaulting to null, and the array item type goes in a @var comment such as list<string>.
Example
Inputs: Root type name = Root. Result: Generated code: <?php declare(strict_types=1); final class Root { public int $id; public string $name; public string $email; public bool $isActive; // JSON key: "is_active" public float $balance; /** @var list<string> */ public array $tags; public Address $address; /** @var list<Order> */ public array $orders; } final class Address { public string $city; public string $postalCode; // JSON key: "postal_code" } final class Order { public string $orderId; // JSON key: "order_id" public float $total; public ?string $coupon = null; }; Types generated: 3; Fields: 13.
{
"id": 1024,
"name": "Sara Ahmed",
"email": "sara@example.com",
"is_active": true,
"balance": 1520.75,
"tags": ["vip", "riyadh"],
"address": { "city": "Riyadh", "postal_code": "12211" },
"orders": [
{ "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
{ "order_id": "A-2", "total": 120, "coupon": null }
]
}<?php
declare(strict_types=1);
final class Root
{
public int $id;
public string $name;
public string $email;
public bool $isActive; // JSON key: "is_active"
public float $balance;
/** @var list<string> */
public array $tags;
public Address $address;
/** @var list<Order> */
public array $orders;
}
final class Address
{
public string $city;
public string $postalCode; // JSON key: "postal_code"
}
final class Order
{
public string $orderId; // JSON key: "order_id"
public float $total;
public ?string $coupon = null;
}Limits and caveats
- Needs PHP 8.0 or newer because of the mixed type.
- The classes do not fill themselves from JSON; use json_decode and assign the properties, or a hydration library.
Questions about this tool
Why array with a @var comment?
PHP has no typed arrays, so the item type goes in a comment read by tools such as PHPStan and Psalm.
Why ListItem instead of List?
list is a reserved word in PHP and cannot name a class, so Item is appended.
Is my JSON sent to a server?
No. The conversion runs in your browser and the JSON never leaves your device.
