JSON to PHP Converter
PHP classes with typed properties and fromArray(), generated from a JSON sample
Input
Output
What does the JSON to PHP converter generate?
json_decode() gives you nested arrays or stdClass objects, which means no autocomplete and no type checks. Paste a JSON sample here and you get one PHP class per object in a single file: <?php, declare(strict_types=1);, your namespace if you set one, and public properties with typed declarations like public string $subscriberId; and public ?Customer $customer;.
Each class also gets a static fromArray(array $data): self that fills it from json_decode($json, true), including nested objects (Account::fromArray(...)) and lists of objects (array_map(fn ($item) => Device::fromArray($item), ...)). PHP has no typed arrays, so array properties are declared array with a /** @var Device[] */ docblock that PhpStorm, PHPStan and Psalm understand. Values that were only null, or whose type varied, become mixed.
Turn off Typed Properties for untyped properties with @var docblocks. Turn off Nullable Types if you'd rather not have ?string on fields that were null or missing in the sample. Property names keep the JSON key when it's a valid PHP name, otherwise they're camelCased (first name becomes $firstName) and fromArray() still reads the original key. The output is checked with php -l and by actually running fromArray() on the input. It all runs in your browser.
How to use it
The classes update as you type.
Paste a JSON sample
Paste into the Input panel, Upload a .json or .txt file, or click Sample. Example input:
{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}Set Class Name for the root class and Namespace (for example App\Dto) if you want one. Invalid JSON? Check it with the JSON Validator.
Read the classes
The Output panel shows nested classes first and the root class last, with a comment showing how to build it.
Copy or download
Copy the code or Download a .php file, then $order = RootObject::fromArray(json_decode($json, true));. To follow PSR-4 autoloading, move each class into its own file.
Where this saves time
Typed DTOs for API clients
Wrap the response of a third-party API in real classes, so your Guzzle client returns an Order with a Customer instead of an array you have to remember the keys of.
Laravel and Symfony request payloads
Generate a DTO for an incoming webhook or JSON request body, then hydrate it from the decoded payload in your controller. The classes are framework-free, so they work in either.
Static analysis
Typed properties and @var docblocks give PHPStan and Psalm something to check, catching typos in property names before they reach production.
Catching type drift
With typed properties and strict_types, fromArray() throws a TypeError if the API suddenly sends a string where an int used to be, rather than letting the bad value through.
Common Questions
Which PHP version do I need?
PHP 7.4 for typed properties and the fn arrow functions in fromArray(), and PHP 8.0 if the output contains mixed. The untyped mode works on older versions apart from the arrow functions.
Does it work with json_decode() objects as well as arrays?
fromArray() expects the associative array form, so decode with json_decode($json, true). If you already have a stdClass, cast it with (array) at each level, or just decode again.
How are big integers handled?
Integers that fit in 64 bits are typed int. Larger ones are typed float, because that's what json_decode() returns for them; pass JSON_BIGINT_AS_STRING and change the type to string if you need every digit.
Why are the typed properties not initialized?
Typed properties without a default stay uninitialized until fromArray() sets them. That is deliberate: reading one that was never set throws an Error instead of silently returning null.
What if the JSON root is an array?
The objects in it become the root class, and a comment shows the array_map() call that builds a list of them. A root that is null, a string or a list of plain values gives a short message instead of code.
Is my JSON uploaded?
No. The classes are generated by JavaScript in your browser and nothing is sent to a server. Input is capped at 256 KB.
Other JSON Tools You Might Need
Need the same payload in another language, or want to validate it first?