JSON to PHP Converter

PHP classes with typed properties and fromArray(), generated from a JSON sample

Input

Output

Paste your JSON on the left and the PHP code shows up here.

Or drop a file on this box, , or .

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. Feed it one representative payload and it writes a PHP class for every object, all in one 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 see ?string on a property whose value was null in one record or absent from another. 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. While building the generator we linted its output with php -l and ran fromArray() on real payloads. On this page the classes are generated client-side; no PHP interpreter is involved.

How to use it

The classes update as you type.

1

Paste a JSON sample

Paste or type into the Input panel, Upload a .json or .txt, or click Sample. A checkout order with a nested customer and an unused coupon field makes a good test:

{"orderId":"ORD-7781","total":129.95,"customer":{"name":"Ava Chen","vip":true},"couponCode":null}

Class Name names the root class. Call it Order and the input above gives a separate Customer class plus public mixed $couponCode;, because a value that was only ever null has no type to infer. Fill in Namespace (say App\Dto) if your autoloader expects one. If the output panel reports invalid JSON, the JSON Validator pinpoints the problem.

2

Read the classes

The Output panel shows nested classes first and the root class last, with a comment showing how to build it.

3

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.

My API returns a JSON array of orders. Does that work?

Yes. The objects in the array become the root class, and a comment at the top shows the call that builds the list, for example array_map(fn ($row) => Order::fromArray($row), $data). Paste [1, 2, 3] instead and there is nothing to model, so the panel says Nothing to generate: the JSON root is an array of numbers.

Is my JSON uploaded?

No. The PHP is written by script in this tab, and the order data you paste never goes to a server. Anything larger than 256 KB is turned away with a note asking for a smaller sample; a few array items tell the generator as much about the types as a full export.

Other JSON Tools You Might Need

If the data arrives as XML, XML to PHP is the sibling page. When json_decode keeps returning null, the validator and formatter show you why; the language pages build the same order for a front end or another service.