JSON to PHP: typed classes you can fill with json_decode
Paste a sample response and get PHP classes that read it, keep its numbers intact and write it back out.
Paste or upload your JSON
Start with real JSON data, ideally a response with every field filled in. Types come from the values you paste, so a list with several items gives better results than a single one.
Example: try this JSON
{
"userId": 12345,
"username": "phpdev",
"email": "dev@example.com",
"isActive": true,
"preferences": ["email_notifications", "newsletter", "api_access"],
"profile": {
"firstName": "Laravel",
"lastName": "Developer",
"age": 28,
"country": "USA"
}
}Pick a class name and getters
There are two settings above the editors, and both change the output straight away.
Read the generated classes
All classes go into one file. fromArray() reads each key by its original name, so a key like order_id still lands in $orderId, and toArray() puts the original names back.
Output for the example above (first part)
<?php
// Needs PHP 8.0 or newer.
// Read JSON into objects with:
// $data = Root::fromJson($json);
// and write it back with $data->toJson().
class Root
{
private int $userId;
private string $username;
private string $email;
private bool $isActive;
/** @var string[] */
private array $preferences;
private Profile $profile;
public static function fromArray(array $data): self
{
$instance = new self();
$instance->userId = $data['userId'];
$instance->username = $data['username'];
$instance->email = $data['email'];
$instance->isActive = $data['isActive'];
$instance->preferences = $data['preferences'];
$instance->profile = Profile::fromArray($data['profile']);
return $instance;
}
public static function fromJson(string $json): self
{
return self::fromArray(json_decode($json, true, 512, JSON_THROW_ON_ERROR | JSON_BIGINT_AS_STRING));
}
public function toArray(): array
{
return [
'userId' => $this->userId,
'username' => $this->username,
'email' => $this->email,
'isActive' => $this->isActive,
'preferences' => $this->preferences,
'profile' => $this->profile->toArray(),
];
}
public function toJson(): string
{
return json_encode($this->toArray(), JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
public function getUserId(): int
{
return $this->userId;
}
public function setUserId(int $value): void
{
$this->userId = $value;
}
// ... getters and setters for the other properties, then class Profile with the same methodsCopy or download the PHP file
Save the file in your project, require it (or move each class into its own file if you use Composer autoloading) and load the data.
What is JSON to PHP Conversion?
json_decode gives you nested arrays or stdClass objects, which work but say nothing about what each field holds. Typed classes catch a missing or misspelled field the moment the data comes in, and your editor can autocomplete them.
Writing those classes by hand for a big API response takes a while, so this page writes them from a real sample. Check the JSON first with the JSON Validator if it was copied from a log, or try XML to PHP when your data comes as XML.
Frequently Asked Questions
How do I load JSON into the generated classes?
Call Root::fromJson($json) with the raw JSON text. It runs json_decode with JSON_THROW_ON_ERROR, so broken JSON throws a JsonException instead of quietly returning null, and then fills in every property, turning nested objects into the nested classes. If you already have an array from json_decode($json, true), pass it to Root::fromArray() instead. $data->toJson() writes it back out using the original JSON keys.
Which PHP version do I need?
PHP 8.0 or newer. The properties use typed declarations, and some fields use union types such as int|string or the mixed type, which arrived in PHP 8.0. Nothing else is required: no Composer packages, just the json extension that every PHP build ships with.
How are int, float and string chosen?
From how each value is written in your JSON, across every item of a list. Whole numbers become int, which in PHP is 64-bit, so an ID like 8901240544102066246 keeps every digit. Numbers written with a decimal point or exponent, like 14.50 or 1e3, become float, and a field with 3 in one item and 14.5 in another becomes float too. Quoted text such as "00501" stays a string, so the leading zeros survive.
What about numbers too big for PHP?
A whole number past 9223372036854775807 does not fit in PHP's int. Plain json_decode turns it into a rounded float, so the generated fromJson() passes JSON_BIGINT_AS_STRING and the property is typed int|string: you get the exact digits as a string, and toJson() writes them back as a string. A comment at the top of the file mentions it when your data has such a number.
What happens when items of a list have different fields?
All items are merged into one class. A field that is missing from some items, or is null in any of them, becomes nullable and defaults to null, so fromArray() never trips over it. A field that holds different kinds of values, say a number in one item and text in another, is typed mixed.
How are odd JSON keys turned into PHP names?
Keys are split into words and joined in camelCase, so "order_id" becomes $orderId. Keys that start with a digit get a field prefix, and keys that end up with the same name, like "first-name" and "first_name", get a number. Class names that PHP reserves or already defines, such as String, List or Exception, get Model added. fromArray() and toArray() still use the original keys, so the JSON reads and writes correctly.
Is my JSON sent anywhere?
No. The classes are built in your browser as you type, and nothing you paste or upload is sent to our server or kept after you leave the page.
Related Tools
JSON to C
Convert JSON data to C structs with parsing functions and memory management
JSON to C++
Convert JSON data to modern C++ classes with STL containers and smart pointers
JSON to C#
Convert JSON data to C# classes with properties and attributes
JSON to Python
Convert JSON data to Python dataclasses with type hints
JSON to Java
Convert JSON data to Java classes with getters, setters, and annotations
JSON to TypeScript
Convert JSON data to TypeScript interfaces and types