Free tool · runs in your browser
🧩 JSON to PHP Class Converter
Paste JSON and get typed PHP 8 classes (readonly DTOs with nested classes and a fromArray() factory) or a PHP array literal, ready to copy into your project.
Runs in your browser: what you enter is never sent anywhere. We only count anonymous usage, such as which buttons are used, to improve the tools.
Why typed DTOs instead of arrays
Arrays from json_decode() give you no autocompletion, no type checks and no guarantee a key exists. A small readonly class per shape turns API responses and request payloads into objects your IDE, PHPStan or Psalm understand, and a typo becomes an error at the boundary instead of a silent null three layers deeper.
Using the generated code
- Save each class in its own file under the namespace you chose (PSR-4: one class per file).
- Create objects from JSON with
Order::fromArray(json_decode($json, true, flags: JSON_THROW_ON_ERROR)). - Rename classes generated from keys (for example
Item) to match your domain. - For untrusted input, validate before mapping, or use a mapper library such as Valinor or Symfony Serializer.
Frequently asked questions
How do I convert JSON to a PHP class?
Paste a sample JSON object. The tool infers each field’s type, turns nested objects into their own classes, and generates PHP 8.2 readonly classes with constructor promotion and a fromArray() factory you can call with json_decode($json, true).
How are optional and null fields handled?
If a key is null, or missing from some items in a list, its property becomes nullable (?string) and fromArray() falls back to null. Include several array items in your sample so optional fields are detected.
How do I convert JSON to a PHP array?
Use json_decode($json, true) at runtime. To embed data in code, switch the output to “PHP array” to get the same data as a PHP array literal with short array syntax.
Which PHP version do the classes need?
Readonly classes need PHP 8.2 or later. Turn off “readonly class” to get readonly properties instead, which work from PHP 8.1. Constructor promotion and named arguments need PHP 8.0.