JSON to PHP Converter
Generate PHP classes from JSON data. Paste your JSON:
Generate PHP Classes from JSON
This tool converts JSON data into PHP class definitions. It supports modern PHP 8+ features like constructor property promotion and typed properties.
Output Options
PHP 8+ Constructor Promotion
Concise syntax with properties declared directly in the constructor:
class User
{
public function __construct(
public int $id,
public string $userName,
public bool $isActive
) {}
}Traditional PHP Class
Classic style with separate properties and constructor:
class User
{
private int $id;
private string $userName;
private bool $isActive;
public function __construct(int $id, string $userName, bool $isActive)
{
$this->id = $id;
$this->userName = $userName;
$this->isActive = $isActive;
}
}Type Mapping
| JSON | PHP |
|---|---|
| string | string |
| integer | int |
| decimal | float |
| boolean | bool |
| null | ?string |
| array | array |
| object | Nested class |
Using Generated Classes
// Create from array
$data = json_decode($jsonString, true);
$user = new User(
$data['id'],
$data['userName'],
$data['isActive']
);
// Access properties
echo $user->userName;With JSON Serialization
// Add JsonSerializable interface
class User implements JsonSerializable
{
// ... properties and constructor ...
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'userName' => $this->userName,
'isActive' => $this->isActive,
];
}
}
// Serialize back to JSON
echo json_encode($user);Related Tools
- JSON Validator — Validate JSON before converting
- JSON to Java — Generate Java POJOs
- JSON to Python — Generate Python dataclasses
- JSON to C# — Generate C# classes
Frequently Asked Questions
What PHP version is required?
Constructor promotion requires PHP 8.0+. Typed properties require PHP 7.4+. Disable these options for older PHP versions.
How do I handle arrays of objects?
PHP doesn't support generic array types in type hints. Arrays are typed as array. Add PHPDoc annotations manually for IDE support:/** @var User[] */
Common Mistakes & Pro Tips
- Typed properties throw if null lands in a non-nullable type — Since PHP 7.4 you can declare `public int $age;`, but assigning `null` to it (e.g. from a nullable JSON field) throws a `TypeError`. Mark fields that may be null as nullable with a leading `?` (`public ?int $age;`), which also makes them default to an uninitialized state you should guard against.
- json_decode gives float for decimals, int for whole numbers — `json_decode` maps JSON integers to PHP `int` and any number with a decimal/exponent to `float`. Very large integers beyond PHP_INT_MAX become `float` (losing precision) unless you pass the `JSON_BIGINT_AS_STRING` flag to keep them as strings. Type money fields as `string` and use BCMath/`brick/math` rather than `float`.
- json_decode(true) gives arrays, not your classes — `json_decode($json, true)` returns nested associative arrays, and `json_decode($json)` returns generic `stdClass` objects — neither produces instances of your generated classes automatically. To hydrate typed objects you must map fields manually in a constructor/factory, or use a library like Symfony Serializer, JMS Serializer, or `cuyz/valinor`.
- Map JSON keys when names differ; mind reserved words — PHP has no built-in JSON property-name attribute in core, so if a JSON key differs from your property name (e.g. snake_case to camelCase) you map it in your hydration code or via a serializer's attributes (Symfony's `#[SerializedName]`). JSON keys that are PHP reserved words or contain hyphens can't be property names — rename and map them explicitly.
- Empty arrays and dates stay loose — PHP arrays are untyped, so a JSON array becomes `array` with no element type guarantee — document the element type in a `@var Item[]` PHPDoc for IDE/static-analysis support. Date strings remain `string`; convert to `DateTimeImmutable` yourself in the constructor, since `json_decode` never produces date objects.
Frequently Asked Questions
Should I generate classes with typed properties or constructor promotion?
PHP 8.0+ constructor property promotion (`public function __construct(public int $id, public ?string $name = null) {}`) is the most concise way to define DTOs and is widely preferred. On PHP 7.4 use plain typed properties and a separate constructor. For readonly immutability, PHP 8.1's `readonly` modifier pairs well with promotion (`public readonly int $id`).
How do I handle optional or nullable fields?
Declare the property nullable with a leading `?` and give it a default of `null` in a promoted constructor (`public ?string $bio = null`) so it can be omitted. Without the `?`, assigning a null JSON value to a typed property throws a `TypeError`, and an uninitialized typed property throws `Error` on read. The default makes the constructor argument optional.
How do I actually turn JSON into these objects?
`json_decode` alone won't create your classes — it returns `stdClass` or arrays. Write a static factory like `User::fromArray(json_decode($json, true))` that pulls each key and passes it to the constructor, or use a serializer (Symfony Serializer, JMS, valinor) that maps and validates types for you. For nested objects, recurse into child factories.
Why might a large integer ID come back as a float or string?
PHP integers are platform-sized (usually 64-bit), and JSON numbers above `PHP_INT_MAX` overflow to `float`, silently losing precision. Pass `JSON_BIGINT_AS_STRING` to `json_decode` so oversized integers are preserved as strings, then type the property `string`. This is common with Twitter/Discord-style snowflake IDs.
Do I need a serializer library or is core PHP enough?
Core `json_decode`/`json_encode` is enough for simple flat structures where you hydrate manually. For deeply nested objects, type coercion, validation, and snake_case-to-camelCase mapping, a library like Symfony Serializer or `cuyz/valinor` saves a lot of boilerplate and catches type mismatches. Choose based on how much mapping and validation you need.
Is my JSON sent to a server?
No. The PHP class generation happens entirely in your browser, so your JSON never leaves your machine. That makes it safe to paste internal payloads or data with credentials, and it works offline once the page is loaded.