Skip to content
100% local

JSON to PHP class

Turn a JSON sample into a typed PHP class with a constructor, fromArray and toArray.

Input
Output

JSON to PHP class

Paste a JSON object or array and this tool infers a type for every field, then generates a ready-to-use PHP class: typed properties, a constructor, a static fromArray() factory and a toArray() method for turning the object back into a plain array. Nested objects become their own classes automatically, and identical shapes are reused instead of duplicated, so a payload with several similarly-structured sections still produces a compact file.

Class name and namespace name the root class and, optionally, wrap it in a PHP namespace. PHP 8 promoted constructor writes properties directly in the constructor signature instead of declaring them separately — turn it off for a classic explicit constructor. Generate getters and setters adds accessor methods (isX() for booleans, getX()/setX() otherwise) instead of exposing properties directly. declare(strict_types=1) adds PHP's strict type header, and docblock comments annotate every property with its inferred type. Array fields decides whether a list is typed as a plain array or documented with its element type, such as string[], since PHP has no generics for that in the type hint.

A field seen as null, or missing from some elements of an array of objects, is generated as nullable — with a safe isset() check when it needs to build a nested object or map a list, and a null-coalescing default otherwise. A key that collides with a PHP keyword or starts with a digit is renamed to a valid property name, and every generated property keeps the original JSON key as its array key in fromArray() and toArray().

Everything runs locally in your browser — the JSON you paste, which may include real API responses or config data, is never uploaded anywhere. Copy the generated class, download it as a .txt file, or send the output back into the input to refine it further.

FAQ

What PHP version does the generated code target?
PHP 8.0 or later, since promoted constructor properties and the mixed type are PHP 8 features. Turn off the promoted-constructor option if you need PHP 7 compatible output.
How are nested objects handled?
Each distinct object shape becomes its own class, with its own fromArray() and toArray() methods. Two fields with an identical shape reuse the same generated class instead of duplicating it.
What happens to a field that is sometimes null or missing?
It is generated as a nullable, typed property. Scalars default to null with the null-coalescing operator; nested objects and lists of objects use an isset() check so fromArray() never calls a method on missing data.
Can I generate multiple classes from one JSON sample?
Yes. Every nested object in the sample produces its own class in the same file, in dependency order, so the file is ready to use as-is.
Is my JSON uploaded anywhere?
No. The class generation runs entirely in your browser — your JSON never leaves your device.