{ }jsonkit
Format
Validate
Convert
Query
Generate

JSON to PHP class generator

Create typed PHP 8 DTO classes and JSON key maps.
353 B · paste or drop a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
✓ complete353 B → 1141 B0.0 ms

About JSON to PHP

The JSON to PHP generator creates typed PHP 8 DTO classes from an example document. Nested objects become final classes and scalars get native property types. Arrays get list docblocks for PHPStan or Psalm. Missing or null values become nullable constructor parameters.

Models use constructor property promotion to stay short. Readonly mode targets immutable DTOs. The optional JSON key map shows how camelCase property names match keys such as order_id or created-at, for use in your own hydrator or serializer.

Runs in your browser. Nothing is uploaded. Note that PHP cannot fill objects from JSON based on type declarations alone. Connect the generated classes to your serializer, framework normalizer or a factory method.

How it works

Infer native PHP 8 property types

Strings become string, booleans bool, whole numbers int and decimals float. Nested objects get their own classes. Nulls and keys missing from some objects in a sampled array become nullable promoted properties.

Describe array element types

PHP's array type cannot declare its element type. So constructors use array plus a list<T> docblock. Static analyzers and IDEs then know the list holds scalars or nested model objects.

Use readonly DTO classes

Readonly mode writes final readonly classes for immutable DTOs and needs PHP 8.2 or newer. Turn it off for PHP 8.0 or 8.1, or when properties must change after construction.

Preserve original JSON key names

Property names are cleaned up to safe camelCase. The JSON key map method returns pairs of original key and property name that a custom hydrator can use. It is metadata, not an automatic deserializer.

Frequently asked questions

Which PHP version does the generated code require?

Constructor property promotion needs PHP 8.0. The readonly class option needs PHP 8.2, so turn it off for PHP 8.0 or 8.1. Everything else uses standard PHP 8 features such as strict types and nullable property syntax.

Will json_decode return these classes automatically?

No. json_decode returns arrays or stdClass objects. Use a framework serializer, a reflection-based hydrator or a factory that builds the generated classes. The optional key map helps translate wire names.

Why are arrays documented with list types?

PHP only checks for array at runtime. A list<T> docblock keeps the element type for PHPStan, Psalm and IDE completion, without pretending PHP enforces generic arrays.

Does the PHP generator send JSON anywhere?

No. Parsing, type inference and PHP code generation run in your browser.

Sources & last verified

We checked this tool's behavior and the claims on this page against the specifications below on . The tool runs in your browser, so the output you see is exactly what the engine produced.

Found a wrong result or an outdated claim? Report an error and we will check it again.