JSON to PHP
Generate typed PHP classes with fromArray() from JSON
Paste JSON or load an example to generate PHP code.
How to use
- 1.Paste JSON, upload or drop a .json file, or load one of the examples.
- 2.Set the root class name and an optional namespace such as App\Models.
- 3.Choose the class style and whether to write typed properties and PHPDoc.
- 4.Read the notes, then copy the code or download Models.php. Decode with json_decode($json, true) and call Root::fromArray($data).
What JSON to PHP generates
JSON to PHP turns an API response or any JSON sample into PHP classes. Constructor promotion writes public typed constructor parameters (PHP 8.0+); the public-properties style writes typed properties and fills them in fromArray(). Required parameters come before optional ones, optional values default to null, and PHPDoc list<Type> annotations describe array items that native types cannot.
How types are inferred
- •Every array item is inspected, not just the first one, and objects in the same array are merged.
- •A key missing from some objects is optional; a key that is sometimes null is nullable. The two are kept apart.
- •1 and 2.5 in the same position widen to a decimal number; other mixes are reported.
- •Empty arrays and values that are only ever null have no type evidence, so a generic type is used and a note explains why.
- •Type mapping: string → string, integer → int, decimal → float, boolean → bool, array → array with list<T> PHPDoc, object → class, null observed → ?T, mixed scalars → union such as string|int, unknown → mixed.
Limits and privacy
A sample shows what the data looked like once, not the full API contract. Review optional fields, generic types and number widths before relying on the code. Input is limited to 5 MB and 256 nesting levels; from 2 MB, generation waits for the Generate button. Everything runs in your browser and the JSON is never uploaded. Integers beyond PHP_INT_MAX are typed mixed, because json_decode turns them into floats unless JSON_BIGINT_AS_STRING is used.
