Output will appear here
Output will appear here
Paste JSON and get typed PHP classes with readonly promoted constructor properties and a static fromArray() method. Feed it the result of json_decode($json, true) and nested objects and arrays of objects are built into their own classes. Missing or null keys become ?type. The output targets PHP 8.1 and declares strict_types=1.
By Ankit Aglawe · Updated · Runs entirely in your browser
shipping_address becomes a ShippingAddress object via ShippingAddress::fromArray(), and line_items is mapped to LineItem objects with array_map. total_price is a string in the JSON, so it stays string; the generator does not guess that it is money. note is only ever null, so it is mixed.
{
"id": 5531204,
"email": "[email protected]",
"total_price": "129.00",
"financial_status": "paid",
"created_at": "2024-08-30T11:20:44-04:00",
"shipping_address": { "city": "Portland", "zip": "97205", "country_code": "US" },
"line_items": [
{ "sku": "BAG-OL", "quantity": 1, "price": 89.5 },
{ "sku": "STRAP-2", "quantity": 2, "price": 19.99, "gift_card": false }
],
"note": null
}<?php
declare(strict_types=1);
class ShippingAddress
{
public function __construct(
public readonly string $city,
public readonly string $zip,
public readonly string $country_code,
) {
}
public static function fromArray(array $data): self
{
return new self(
city: $data['city'],
zip: $data['zip'],
country_code: $data['country_code'],
);
}
}
class LineItem
{
public function __construct(
public readonly string $sku,
public readonly int $quantity,
public readonly float $price,
public readonly ?bool $gift_card,
) {
}
public static function fromArray(array $data): self
{
return new self(
sku: $data['sku'],
quantity: $data['quantity'],
price: $data['price'],
gift_card: $data['gift_card'] ?? null,
);
}
}
class Root
{
public function __construct(
public readonly int $id,
public readonly string $email,
public readonly string $total_price,
public readonly string $financial_status,
public readonly string $created_at,
public readonly ShippingAddress $shipping_address,
public readonly array $line_items,
public readonly mixed $note,
) {
}
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
email: $data['email'],
total_price: $data['total_price'],
financial_status: $data['financial_status'],
created_at: $data['created_at'],
shipping_address: ShippingAddress::fromArray($data['shipping_address']),
line_items: array_map(fn (array $e) => LineItem::fromArray($e), $data['line_items']),
note: $data['note'] ?? null,
);
}
}A top-level array gives one class plus a Root[] comment. sticky is in one post only, so it is ?bool and filled with $data['sticky'] ?? null, which avoids an "undefined array key" warning on the other posts. tags is a plain array, since PHP has no typed arrays.
[
{ "id": 311, "slug": "hello-world", "title": "Hello world", "status": "publish", "featured_media": 0, "tags": [4, 9] },
{ "id": 312, "slug": "draft-ideas", "title": "Draft ideas", "status": "draft", "featured_media": 77, "tags": [], "sticky": true },
{ "id": 313, "slug": "release-notes", "title": "Release notes", "status": "publish", "featured_media": 0, "tags": [9] }
]<?php
declare(strict_types=1);
class Root
{
public function __construct(
public readonly int $id,
public readonly string $slug,
public readonly string $title,
public readonly string $status,
public readonly int $featured_media,
public readonly array $tags,
public readonly ?bool $sticky,
) {
}
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
slug: $data['slug'],
title: $data['title'],
status: $data['status'],
featured_media: $data['featured_media'],
tags: $data['tags'],
sticky: $data['sticky'] ?? null,
);
}
}
// Root type: Root[]Every code generator on this site is run on the same awkward input: a 64-bit id, a float written as 1.0, a null, a date string, a reserved word as a key, an empty array, a nested array and an array of objects whose keys differ. This is the exact output for PHP, generated when the page was built.
{
"id": 9007199254740993,
"price": 19.99,
"ratio": 1.0,
"deleted_at": null,
"created_at": "2024-01-15T10:30:00Z",
"class": "premium",
"tags": [],
"matrix": [[1, 2], [3, 4.5]],
"items": [{ "sku": "A1", "qty": 1 }, { "sku": "B2", "qty": 2, "note": "gift" }]
}<?php
declare(strict_types=1);
class Item
{
public function __construct(
public readonly string $sku,
public readonly int $qty,
public readonly ?string $note,
) {
}
public static function fromArray(array $data): self
{
return new self(
sku: $data['sku'],
qty: $data['qty'],
note: $data['note'] ?? null,
);
}
}
class Root
{
public function __construct(
public readonly int $id,
public readonly float $price,
public readonly int $ratio,
public readonly mixed $deleted_at,
public readonly string $created_at,
public readonly string $class,
public readonly array $tags,
public readonly array $matrix,
public readonly array $items,
) {
}
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
price: $data['price'],
ratio: $data['ratio'],
deleted_at: $data['deleted_at'] ?? null,
created_at: $data['created_at'],
class: $data['class'],
tags: $data['tags'],
matrix: $data['matrix'],
items: array_map(fn (array $e) => Item::fromArray($e), $data['items']),
);
}
}A field that is null in some samples becomes ?string, ?int and so on. A field that is null in every sample, like deleted_at, becomes mixed, because ?mixed is not valid PHP and there is no other evidence of its type.
Whole numbers become int and decimals become float. 64-bit PHP holds 9007199254740993 exactly as an int after json_decode. One trap: the browser reads 1.0 as the integer 1, so ratio is typed int, but PHP decodes 1.0 as a float, and with strict_types=1 that is a TypeError. Change such fields to float.
A key missing from some objects gets a nullable type, and fromArray() reads it with ?? null (or isset() for nested objects) so an absent key becomes null instead of a warning. Every constructor parameter stays in JSON order, so named arguments line up with the source.
PHP allows keywords as property names, so class stays $class and the named argument class: is valid. Characters PHP does not allow in a name are replaced: user-name becomes $user_name, while fromArray() still reads $data['user-name'].
Timestamps stay string. If you want objects, change the type to \DateTimeImmutable and wrap the value in new \DateTimeImmutable(...) inside fromArray().
Arrays are typed array. Only arrays of objects are converted, through array_map and the item class. Empty arrays like tags and nested arrays like matrix are passed through as they are. Add a @var list<Item> docblock if you use PHPStan or Psalm.
| JSON value | PHP type | Notes |
|---|---|---|
| string | string | Dates included |
| whole number | int | 64-bit on 64-bit PHP |
| number with a fraction | float | |
| true / false | bool | |
| null in some samples | ?T | |
| always null | mixed | |
| key missing in some objects | ?T | Read with ?? null |
| object | class Name | Built with Name::fromArray() |
| array of objects | array | Mapped to objects with array_map |
| other arrays | array | Passed through |
Paste a JSON object or array into the editor. The converter processes nested objects, arrays, and all JSON primitive types including null values.
The output displays PHP classes with typed properties (PHP 7.4+), constructor initialization, and getter methods. Nested objects generate separate classes with proper type hints.
Copy the generated code into your project. Use json_decode() to parse raw JSON and then instantiate the classes with the decoded data for strongly typed access.
Paste the JSON, copy the classes, then call Root::fromArray(json_decode($json, true, flags: JSON_THROW_ON_ERROR)). The true matters: fromArray() expects an associative array, not a stdClass.
You do not need a tool for that: json_decode($json, true) returns a nested associative array. This page is for when you want typed classes on top of that array.
PHP 8.1 or later. It uses readonly promoted constructor properties (8.1) and named arguments (8.0).
Yes, the classes are plain PHP with no framework dependency. In Laravel, Root::fromArray($request->json()->all()) or Root::fromArray(Http::get($url)->json()) both work.
Properties are declared readonly, so the object is immutable after construction. Remove readonly if you need to mutate it.
Map each element: array_map(fn (array $e) => Root::fromArray($e), json_decode($json, true)).
Need several formats at once? Convert All shows every output from one paste. Or browse all JSON converters.