Advertisement

JSON to PHP Class

Generate PHP classes from JSON with typed properties. Nested classes, array types, and nullable fields follow modern PHP 7.4 plus conventions.

JSON input

Paste JSON, optionally name the root type, then generate.

Your PHP class code will appear here.
Advertisement

Modern typed PHP

Since PHP 7.4, properties can carry types, turning loose stdClass blobs into checked structures. The generator maps JSON types onto string, int, float, bool, array, and nested classes with nullability inferred.

A foundation for API clients

Use the generated classes as the return types of your API client methods. Typed properties catch shape mismatches early and give your IDE the information it needs for autocomplete.

JSON to PHP Class FAQs

Which PHP version does the output need?
Typed properties require PHP 7.4 or newer. The generated code avoids newer syntax so it runs on 7.4 through 8.x without changes.
How are optional fields handled?
Uncertain fields become nullable typed properties defaulting to null, like public ?string $nickname = null, which matches how JSON APIs omit keys.
How are nested objects represented?
Each nested object becomes its own class, so an address object becomes an Address class referenced by a typed property.
How are arrays typed?
PHP has one array type, so JSON arrays become public array $tags, optionally documented with a @var annotation describing the element type.
Can I deserialize JSON into these automatically?
Yes, with libraries like Symfony Serializer or JMS Serializer. For quick scripts, json_decode with a manual mapping loop also works fine.
Advertisement