Convert JSON to PHP classes or readonly DTOs with fromArray and JsonSerializable
A free JSON to PHP converter: paste JSON and get PHP classes back instantly. Choose a classic class with typed private properties, getters and setters, or a readonly DTO with constructor-promoted public readonly properties (PHP 8.1+). Both declare strict_types, read the array json_decode gives you with fromArray and fromJson, and implement JsonSerializable so json_encode writes the same document back. Types are inferred from the data and listed in docblocks, nested objects become their own classes, and names PHP reserves (list, match, Exception) are renamed so the file loads. Conversion runs entirely in your browser, so your JSON is never uploaded to a server.
Output formats: Class (Typed private properties with getters and setters), Readonly DTO (Final class with promoted public readonly properties (PHP 8.1+)).
| Option | What it does |
|---|---|
| Root Class Name | Names the top-level Class. |
| Namespace (optional) | Adds a namespace declaration. App\Models, app.models and app/models all give namespace App\Models. |
| Field Naming Convention | One of camelCase, snake_case, PascalCase, Preserve Original. Defaults to camelCase. |
| Type for null values | Either mixed or string, used when a key is null in every sample. |
| Separate Classes | Write each class to its own .php file, each loading the files it refers to with require_once. Off (the default) puts every class in one file, so Copy All pastes into a single file that loads. |
| Map original JSON keys | Read and write each property under the original JSON key. Without it a property only binds to a key spelled exactly like it, so a snake_case key is not read into a camelCase property. |
| JSON value | PHP type |
|---|---|
| string | string |
| integer | int |
| decimal | float |
| true / false | bool |
| ISO-8601 timestamp | string |
| array | array (docblock T[]) |
| object | nested class |
| null or missing in some samples | ?T |
| null | mixed (configurable) |
{
"id": 1,
"product_name": "Wireless Mouse",
"tags": ["electronics"]
}
class Product implements \JsonSerializable
{
private int $id;
private string $productName;
/**
* @var string[]
*/
private array $tags;
/**
* @param string[] $tags
*/
public function __construct(int $id, string $productName, array $tags)
{
$this->id = $id;
$this->productName = $productName;
$this->tags = $tags;
}
/**
* @param array<string|int, mixed> $data
*/
public static function fromArray(array $data): self
{
return new self(
$data['id'],
$data['product_name'],
$data['tags'],
);
}
public static function fromJson(string $json): self
{
return self::fromArray(json_decode($json, true, 512, JSON_THROW_ON_ERROR));
}
public function jsonSerialize(): object
{
return (object) [
'id' => $this->id,
'product_name' => $this->productName,
'tags' => $this->tags,
];
}
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getProductName(): string
{
return $this->productName;
}
public function setProductName(string $productName): void
{
$this->productName = $productName;
}
/**
* @return string[]
*/
public function getTags(): array
{
return $this->tags;
}
/**
* @param string[] $tags
*/
public function setTags(array $tags): void
{
$this->tags = $tags;
}
}
Paste your JSON into the input pane and the PHP classes appear beside it immediately. Set the root class name and an optional namespace, pick Class or Readonly DTO, then copy the code or download it. Load it and call Root::fromJson($json) to get an object, and json_encode($object) to write it back. Nested JSON objects become their own classes and arrays are mapped element by element.
Class gives typed private properties with getters and setters and runs on PHP 7.4 and later. Readonly DTO is a final class whose constructor promotes public readonly properties, so a value cannot change after it is built; it needs PHP 8.1 or later. Both have fromArray, fromJson, toJson and JsonSerializable, so the calling code reads the same.
PHP has no schema-driven deserializer in the standard library, so the classes read the array that json_decode($json, true) returns. Each fromArray builds the object property by property and hands nested objects to their own class, which keeps the code dependency-free.
A key like list or match would become a class name PHP rejects, and one like error or exception would clash with a class PHP already declares, so the class gets a prefix from its parent (RootList). A property named this becomes this_. The JSON key is kept: with Map original JSON keys on, fromArray reads and jsonSerialize writes the original key.
A DateTime reformats a timestamp when it is written back, adding fractional digits or changing the zone, so the document would no longer round-trip. A timestamp stays a string; build a DateTimeImmutable from it where you need one.
No. Parsing, type inference, and code generation all run in your browser using JavaScript. Your JSON is never sent to a remote server, and there is no account to create. The page keeps your last input and settings in your own browser (localStorage) so they survive a reload; nothing is stored anywhere else.
The conversion itself never transmits your JSON, so a confidential payload does not leave your machine. You can verify this: open your browser DevTools, switch to the Network tab, and convert something — no request carries your data. Two things to know: your last input is saved in your browser's localStorage (clear site data to remove it), and the Copy Link button embeds your JSON in the link, so anyone who opens that link, and the server that receives the request, can see it. Only share such links where you are comfortable with that.
No. The generator is free to use with no sign-up and no usage limits.
Each nested JSON object becomes its own named type, derived from the key it appeared under. Arrays of objects are widened across every element, so a field that is a whole number in one element and a fraction in another gets a type that holds both.