JSON to PHP Class
Generate modern PHP 8+ class definitions from JSON data, utilizing constructor property promotion and strict typing.
<?php
class Root {
public function __construct(
public int $id,
public string $name,
public string $email,
public Address $address,
public bool $active
) {}
}
class Address {
public function __construct(
public string $street,
public string $city
) {}
}
About the JSON to PHP Class
Paste a JSON sample and get PHP classes with typed, promoted constructor properties – one class per nested object – ready to map API responses.
Everything you enter is processed in your browser; nothing is uploaded or stored.
How to use it
- Paste a representative JSON object into Input JSON.
- Copy the generated code from PHP Classes.
- Rename classes and adjust types where the sample was ambiguous.
How JSON types become PHP types
Each JSON object becomes a class whose constructor declares one public property per key (PHP 8 constructor property promotion). Types are inferred from the sample: whole numbers become int, decimals float, true/false bool, text string, arrays array, and nested objects get their own class named in PascalCase from the key.
Because types come from one sample, a field that is sometimes null or sometimes a different type needs a nullable (?string) or union type added by hand.
Example
{"id": 7, "price": 49.5, "author": {"name": "Asha"}} produces an Author class with string $name and a root class with int $id, float $price and Author $author.
Frequently asked questions
Which PHP version is the output for?
PHP 8.0 or later, because it uses constructor property promotion.
How do I fill the class from JSON?
Decode with json_decode($json, true) and pass the values to the constructor, or use a mapper library.
Can I get a plain PHP array instead?
json_decode($json, true) already gives an associative array; the classes add type safety.