JSON to PHP Class
Generate typed PHP classes from JSON, in your browser.
What it's for
JSON to typed PHP class
Modern PHP 8.x
Generates code with constructor promotion, readonly properties and typed properties following modern PHP best practices.
100% private
Your JSON (which may contain sensitive data) is processed only in your browser. Never sent to any server.
Correct typing
Automatically detects string, int, float, bool and null. Optional fields are marked as nullable (?type).
Instant
PHP classes ready in milliseconds. No signup, no waiting, no limits.
How it works
Three steps, no hassle
Paste your JSON
Paste any JSON object, simple or nested. The tool analyzes the structure and data types automatically.
PHP class generated
You get a PHP class with typed properties (PHP 7.4+), constructor promotion (PHP 8.0+) and optional getter methods.
Copy and use
Copy the code with one click and paste it into your project. Works with Laravel, Symfony, or plain PHP.
FAQ
Got questions?
PHP 7.4 (released November 2019) introduced typed properties, allowing you to declare the type of each property directly in the class: public int $id; public string $name; public ?float $price;. This is a fundamental improvement over earlier versions where types could only be documented in PHPDoc. Typed properties improve runtime error detection, code readability, and allow IDEs to offer better autocompletion. The nullable type (with ?) indicates the property can be null, useful for optional JSON fields.
Constructor promotion (constructor property promotion), introduced in PHP 8.0 (2020), allows defining and assigning properties directly in constructor parameters, eliminating redundancy. Instead of declaring the property, listing it in the constructor, and assigning it in the body, you can write: public function __construct(public readonly string $name, public int $age) {}. This drastically reduces boilerplate code and is the preferred pattern in modern PHP, especially for Value Objects and DTOs.
Standard getter methods are generated following PHP convention: getName(), getId(), getPrice(). Getters encapsulate access to private or protected properties, allowing transformation logic to be added in the future without changing the class's public interface. If you use constructor promotion with public or readonly properties, getters are optional. For private properties, getters are the only way to access the value from outside the class.
Eloquent models extend Illuminate\Database\Eloquent\Model and are designed to represent database records with integrated ORM. Plain PHP classes (POPOs, Plain Old PHP Objects) are better for DTOs (Data Transfer Objects), API responses, or when there's no persistence involved. If you're consuming an external API and want to model the JSON response, a plain PHP class generated with this tool is the right choice. Laravel API Resources (introduced in Laravel 5.5) also use plain classes to transform Eloquent models to JSON.
For each nested object in the JSON a separate PHP class is generated. For example, if the JSON has {"user": {"address": {"city": "Madrid"}}}, three classes are generated: the root class with $user property of type User, the User class with $address property of type Address, and the Address class with $city property of type string. This follows the single responsibility principle and correctly maps complex API structures. Arrays of objects are typed as array with the element type in PHPDoc.
PHP 8.x evolution, Laravel API resources, and the Symfony serializer
PHP has evolved dramatically from PHP 7.0 (2015) to PHP 8.3 (2023). PHP 7.4 introduced typed properties and the nullsafe operator. PHP 8.0 brought union types, named arguments, match expressions, and constructor promotion. PHP 8.1 added readonly properties, native enums, fibers, and intersection types. PHP 8.2 introduced readonly classes and disjunctive normal form types. PHP 8.3 completed readonly with readonly properties in anonymous classes. This evolution transforms PHP into a language with optional static typing comparable to TypeScript or Kotlin.
In the Laravel ecosystem (the most popular PHP framework with over 75 million downloads), API Resources (introduced in Laravel 5.5, 2017) are the standard way to transform Eloquent models into JSON responses. They extend Illuminate\Http\Resources\Json\JsonResource and allow precise control over which fields to expose. For the opposite side (consuming external APIs), DTOs implemented as plain PHP classes are the recommended pattern. Packages like spatie/laravel-data (2021) combine both concepts: a class that can act as both a DTO and an API Resource simultaneously.
Symfony's Serializer component (also used by API Platform, the most advanced PHP framework for REST/GraphQL APIs) can serialize and deserialize PHP objects to/from JSON, XML, YAML, and CSV. It uses annotations, PHP 8 attributes, or YAML configuration to map properties. JMS Serializer is another popular alternative. In projects consuming APIs, the pattern of generating PHP classes from JSON and then using the Symfony Serializer to hydrate them (deserialization) is a professional workflow that cleanly separates the transport layer (JSON) from the domain layer (PHP objects).