FormatterHQ

JSON to PHP Class Generator

Generate PHP classes from JSON: typed private properties, a constructor, getters, and methods that build objects from json_decode output and validate each value.

Input JSON
Output PHP
Ready.

How to use the JSON to PHP

  1. Paste your JSON sample (an object, or an array of objects) into the Input editor, or use Sample, Upload file or Load URL, or press Sample to try one.
  2. Set the Top-level class name, for example Task; it is converted to PascalCase. Nested types are named after their property keys.
  3. The Types only option does not change PHP output; classes always include validation and conversion methods.
  4. Press Generate (it also re-runs automatically as you edit), then use Copy or Download.

JSON to PHP: typed classes for decoded JSON

json_decode gives you stdClass objects or arrays: no types, no autocompletion, and no guarantee that a field exists. This JSON to PHP generator writes a class per object shape with typed properties, so the rest of your code can depend on a known structure. The classes use PHP 7.4+ typed properties and are self-validating: every value is checked when you build an object from decoded JSON.

Example

{
  "id": 42,
  "title": "Write docs",
  "done": false,
  "priority": 2.5,
  "tags": ["docs"],
  "owner": { "name": "Ada", "email": null },
  "due": "2026-10-01T09:00:00Z",
  "subtasks": [
    { "title": "Outline", "estimate": 2 },
    { "title": "Draft" }
  ]
}

An excerpt of the generated Subtask class:

class Subtask {
    private string $title; // json:title Required
    private ?int $estimate; // json:estimate Optional

    public function __construct(string $title, ?int $estimate) {
        $this->title = $title;
        $this->estimate = $estimate;
    }

    public static function from(stdClass $obj): Subtask { /* ... */ }
    public function to(): stdClass { /* ... */ }
    public function validate(): bool { /* ... */ }
    public function getTitle(): string { /* ... */ }
    public function getEstimate(): ?int { /* ... */ }
}

Each property records its JSON key and whether it is required in a comment. Optional values use nullable types such as ?int. Nested objects are typed with their generated class names, and lists are plain array properties whose elements are checked one by one during conversion.

What the generated methods do

  • from(stdClass $obj) builds an instance from json_decode($json) output, recursively creating nested objects and lists.
  • to() converts the object back to a stdClass ready for json_encode.
  • validate() and per-property validators throw an Exception with a message like Attribute Error:Task::title when a value has the wrong type.
  • Getters for every property, and sample() methods that create example instances for tests.
$task = Task::from(json_decode($json));   // stdClass, not associative array
echo $task->getTitle();
echo json_encode($task->to());

Note that from() expects objects, so call json_decode without the true flag.

Generated code size

Because every property has its own from, to, validate, getter and sample method, the output is long: the small example above produces several hundred lines. That is the price of explicit runtime validation. If you only want a lightweight DTO, keep the property declarations and constructor and delete the rest, or turn them into a modern PHP 8 class with constructor promotion:

final class Subtask {
    public function __construct(
        public readonly string $title,
        public readonly ?int $estimate = null,
    ) {}
}

Handling validation errors

try {
    $task = Task::from(json_decode($body));
} catch (Exception $e) {
    error_log('Invalid task payload: ' . $e->getMessage());
    http_response_code(422);
}

Catching the exception at the point where external data enters your application keeps bad payloads from spreading. For stricter decoding of the raw string, pass JSON_THROW_ON_ERROR to json_decode so malformed JSON throws a JsonException instead of returning null. In frameworks such as Laravel or Symfony, run this conversion inside a request DTO or form-request class so controllers only ever receive valid, typed objects.

Type mapping

JSONPHP
integer / decimalint / float
boolean / stringbool / string
ISO date-timeDateTime
arrayarray (with element checks)
objecta class
optionalnullable type ?T

Property names are converted to camelCase (user_name becomes $userName); the conversion methods keep using the original keys. Format the result with the PHP Formatter. If you just need a PHP array literal of the data, use JSON to PHP Array. Generation runs in your browser.

Frequently asked questions

Why is the generated PHP code so long?
Every property gets conversion, validation, getter and sample methods so that invalid JSON is rejected with a clear exception. Delete what you do not need, or keep only the typed properties for a simple DTO.
Should I decode JSON as an array or object?
The generated from() methods expect stdClass objects, so call json_decode($json) without the associative flag.
Which PHP version is required?
Typed properties need PHP 7.4 or newer. The code also runs on PHP 8, where you might simplify it with constructor promotion and readonly properties.
Does it work with Laravel or Symfony?
Yes. The classes are plain PHP with no framework dependency and can be used as DTOs in any project.

Last updated October 3, 2026

Related tools