"category": "code-generator",
JSON to PHP Class Generator
"tldr": Paste JSON and get PHP 8 classes with typed public properties under strict_types - nested classes and array docblocks included.
Convert JSON into PHP 8 classes with typed public properties under strict_types. Keys become camelCase properties, nested objects become their own classes, and arrays get @var docblocks recording the element type.
Typed classes turn the classic PHP pitfall - passing associative arrays around and guessing their keys - into IDE-completable, statically analyzable code that PHPStan and Psalm can verify.
How to convert JSON to PHP classes
- 1Paste a representative JSON payload into the input panel.
- 2Each nested object becomes its own class with typed properties.
- 3Array properties carry a @var docblock (e.g. Items[]) so static analyzers know the element type.
- 4Copy the classes into your project and hydrate them from json_decode output (see examples).
Convert JSON to PHP class in code
<?php
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$customer = new Customer();
$customer->name = $data['customer']['name'];
$customer->email = $data['customer']['email'];<?php
use Symfony\Component\Serializer\Serializer;
$order = $serializer->deserialize($json, Root::class, 'json');
echo $order->customer->name;Frequently asked questions
How do I populate these classes from JSON?
json_decode gives you stdClass or arrays, not your classes. Map manually for small payloads, or use a hydrator/serializer: symfony/serializer, jms/serializer, or valinor can deserialize JSON directly into typed objects including nested classes and arrays.
Why public properties instead of getters and setters?
For data-transfer objects, typed public properties (PHP 7.4+) are idiomatic modern PHP - the type system enforces correctness without boilerplate. Add readonly (PHP 8.1+) for immutability, or switch to constructor promotion if you prefer.
json_decode returns null - is my JSON broken?
json_decode returns null both for invalid JSON and for the valid document "null", which makes errors easy to miss. Call json_decode($s, flags: JSON_THROW_ON_ERROR) to get a JsonException with details instead of a silent null.
What PHP version does the output require?
Typed properties need PHP 7.4+, and the mixed type used for null/unknown fields needs PHP 8.0+. For older codebases, replace mixed with a docblock-only type and drop the property type declarations.
Last updated:
You might also need
Free and in-browser, like everything on JSON Console. Browse all tools →