formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

JSON to PHP Class

Paste a sample JSON object below and generate PHP classes with typed public properties (PHP 7.4+). The tool infers a type for every field and creates a named class for each nested object.

Runs entirely in your browser — your JSON is never uploaded to a server. No ads injected into the output, no sign-up. Emits PHP 7.4+ typed properties and a class per nested object, so the shape is clear at a glance.

How to generate a PHP class from JSON

  1. Paste your JSON. Paste one representative JSON object into the box above.
  2. Generate the class. Click Generate PHP class. The tool infers a type for every field and emits a class with typed properties for the root and each nested object.
  3. Hydrate from json_decode. Copy the classes in and assign each property from json_decode($json, true), or use a mapper library.

How the type mapping works

Each JSON value maps to a PHP type: a whole number becomes int, a decimal becomes float, a string becomes string, true/false becomes bool, and null becomes an untyped property. Arrays become array. Every nested object gets its own class with a typed property referencing it.

Arrays and nested objects

Because PHP arrays are untyped, list fields are declared as array, but if the array holds objects the tool still generates a class for that object shape. Property names are sanitized to valid PHP identifiers.

Using the result

Typed properties require PHP 7.4 or newer. To hydrate from a decoded payload, assign each property from json_decode($json, true), or use a mapper library for automatic binding.

FAQ

Which PHP version is required?

Typed properties like 'public int $id;' require PHP 7.4 or newer. On older versions, remove the type declarations and keep the property names.

Why are array fields untyped?

PHP arrays do not carry an element type in a property declaration, so list fields are declared as array. When the array holds objects, the tool still generates a class for that nested shape.

How do I populate an instance?

Decode the JSON with json_decode and assign each property, or use a hydrator/mapper library that binds an associative array onto the typed class.