Monitoring
Solutions
Resources
Free Tools
Pricing
Free Tools / Converters / JSON to PHP Class Generator

JSON to PHP Class Generator

Paste JSON and get typed PHP classes: one per object, with a fromArray() factory that builds the whole tree from json_decode() output — no more guessing array keys.

Runs entirely in your browser — nothing you enter here is sent to a server.

Why turn JSON arrays into classes

json_decode($body, true) gives you nested arrays, and from then on every access is a string key the IDE cannot check: $order['custmer']['email'] fails at runtime, in production, on the one code path nobody tested. A data transfer object moves that knowledge into types. Properties autocomplete, static analysers such as PHPStan and Psalm verify them, and a missing key fails at the boundary — where the data enters — instead of deep inside your logic.

Typed properties

string, int, float, bool, array or another generated class. Values that were null become mixed; optional ones get a nullable type.

Array contents

PHP cannot type the elements of an array, so each list gets a docblock — @var Item[] — that IDEs and analysers understand.

fromArray()

Each class has a static factory that reads the decoded array, including nested objects and lists of objects, and returns a fully built instance.

Two styles

PHP 8 constructor promotion for compact, immutable-friendly classes, or plain public properties if you target older code.

Using the classes

Decode with exceptions enabled and pass the result to the root factory: $order = Root::fromArray(json_decode($body, true, flags: JSON_THROW_ON_ERROR));. With declare(strict_types=1), which the generated file sets, a value of the wrong type raises a TypeError right there.

Then adjust what a sample cannot know. Make the properties readonly if the object should not change. Turn date strings into DateTimeImmutable inside fromArray(). Replace float with integer cents or a money object for prices. And add a namespace and split the classes into files to match your autoloader.

Frequently asked questions

How do I convert JSON to a PHP object?

json_decode($json) returns stdClass objects, and json_decode($json, true) returns arrays. For typed objects, generate classes here and build them with Root::fromArray(json_decode($json, true)).

Which PHP version do the generated classes need?

The constructor promotion style needs PHP 8.0 or later, because of promoted properties, named arguments and the mixed type. The plain style works on PHP 7.4 after removing mixed.

Why does a property have the type mixed?

Its value was null in the sample, so no type could be inferred. Replace mixed with the real type when you know it.

Can I use these classes with Laravel or Symfony?

Yes, they are plain PHP. In Symfony you can instead let the Serializer component fill the same classes, and in Laravel they work well as DTOs between a client and your models.

Related free tools

A calculator answers once. Monitoring answers every minute.

Upzilla checks your sites, servers and APIs around the clock and alerts you the moment something breaks. 10 monitors free, no card required.

Start monitoring free →