toolqik

139 · JSON

JSON to PHP

Generate typed PHP 8 classes with constructor promotion and fromArray() from JSON.

Runs in your browser
Options
PHP code

  

About this tool

json_decode($json, true) gives you nested arrays with no types and no autocompletion. Paste a JSON example and this tool writes typed PHP classes, often called DTOs: one final class per object, with typed properties declared through constructor promotion and a fromArray() factory that maps the decoded array, nested objects included.

What you get

  • Typed properties: string, int, float, bool, nested class types and array for lists, with a @param list<Order> docblock so PHPStan, Psalm and your IDE know the item type.
  • Nullable and optional fields become ?type with a null default, placed after required parameters as PHP 8 requires.
  • readonly (PHP 8.1+) makes the objects immutable; turn it off for PHP 8.0 or mutable objects.
  • fromArray() uses named arguments, so the mapping from JSON keys ($data['user_name']) to camelCase properties (userName) is explicit and easy to adjust.
  • declare(strict_types=1); so type mismatches throw instead of being silently converted.

How types are inferred

  • Every object becomes its own named type. Names come from the key (address → Address), and items of an array get the singular (orders → Order).
  • For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes null becomes nullable.
  • Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
  • Identical nested shapes are reused instead of generating duplicates.

The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.

Use the classes

$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
$user = User::fromArray($data);
echo $user->address->city;

// A top-level list:
$orders = array_map(fn (array $o) => Order::fromArray($o), $data);

In Laravel or Symfony, you can also hand these classes to a serializer (such as symfony/serializer or spatie/laravel-data) instead of using fromArray().

FAQ

How do I convert JSON to a PHP class?

Paste the JSON above. A typed PHP 8 class is generated for each object, with a fromArray() method that builds it from json_decode($json, true).

Which PHP version is required?

PHP 8.0 for constructor promotion and named arguments, and 8.1 if readonly properties are on.

How are arrays of objects typed?

PHP has no generic arrays, so the property is typed array and a @param list<Item> docblock describes the items for static analysers and IDEs. fromArray() maps each item to the class.

Why are optional parameters at the end?

PHP 8 deprecates optional parameters before required ones, so nullable and optional fields are moved after the required fields.

Can I use json_decode objects instead of arrays?

fromArray() expects associative arrays, so decode with json_decode($json, true). For stdClass objects, cast with (array) at each level or change the factory.

esc
↑↓ navigate↵ open