jsontoolskit.org
JSON and JSON Schema utilities, in the browser
Say hi →

JSON to PHP

JSON sample → PHP 8 classes or an array literal · updated 28 July 2026

Generate PHP 8 classes from a JSON sample, using promoted readonly constructor properties and a fromArray() factory — or switch to array literal mode to get the data itself as PHP source, handy for fixtures and config files.

Paste input to start.

How to generate PHP classes from JSON

  1. Paste a JSON sample — ideally an array of several records, so optional fields are detected.
  2. Set the Root type name. Nested objects are named after the key that holds them, singularised for arrays.
  3. Choose Typed classes for a model, or Array literal to embed the data in PHP source.
  4. Set a Namespace if the file belongs in an autoloaded tree.
  5. Copy the code, or download it as a file.

Type mapping

Getting good types out of a sample

Type inference from a sample is only as good as the sample. Three habits make the output far more useful:

Privacy

Nothing is uploaded. Inference and code generation run in this tab. No server sees the payload you paste — which matters, because real API responses tend to contain real user data.

FAQ

Which PHP version do I need?

Constructor property promotion needs PHP 8.0, readonly needs 8.1. Tick No readonly to target 8.0. For 7.x you would need explicit properties and a hand-written constructor.

Why readonly by default?

A model built from a JSON response is a value object — mutating it after construction almost always signals a bug. readonly makes that a compile-time error rather than a debugging session.

Does fromArray recurse into nested objects?

Yes, for object-typed fields it calls the nested class's own fromArray. Arrays of objects are left as plain arrays — map them yourself, since the element type is only known from the docblock.