JSON to PHP Class Converter
Convert JSON to typed PHP 8 classes online with promoted constructor properties and fromArray factories, plus a plain PHP array literal view.
What is the JSON to PHP Class Converter?
The ByteTools JSON to PHP Class Converter turns a sample JSON document into typed PHP 8 classes using constructor property promotion.
- PHP 8 constructor property promotion with typed properties
- Static fromArray() factory per class, using named arguments
- Nullable ?types and ?? null for optional keys
- array_map conversion for lists of nested objects
- Bonus output: the JSON as a plain PHP array literal
- 100% private — JSON never leaves your browser
How to use the JSON to PHP Class Converter
- 1
Paste a JSON sample into the input box.
- 2
Set the root class name.
- 3
Click Generate PHP to build the classes and the array literal.
- 4
Copy either output or download the classes as a .php file.
About the JSON to PHP Class Converter
The ByteTools JSON to PHP Class Converter turns a sample JSON document into typed PHP 8 classes using constructor property promotion. Each class gets a static fromArray() factory that maps the original JSON keys onto camelCase properties, converts nested objects through their own classes and array_maps lists of objects into typed arrays.
It also produces a second output: the same data as a plain nested PHP array literal, ready to paste into a config file or test fixture. Nullable and optional keys become nullable ?types with null-coalescing lookups, so decoding partial data never throws undefined-index warnings.
The tool is aimed at PHP and Laravel developers turning API payloads into DTOs. Everything runs 100% locally in your browser with JavaScript — your JSON is never uploaded or stored anywhere.
Frequently asked questions
How do I convert JSON to a PHP class?
Paste a JSON sample, choose the root class name and click Generate PHP. You get typed PHP 8 classes with promoted constructor properties and fromArray() factories — call Root::fromArray(json_decode($json, true)) to hydrate them.
What PHP version does the generated code need?
PHP 8.0 or newer, because it uses constructor property promotion and named arguments. If you are on PHP 7, use the plain array literal output instead and access values with normal array syntax.
How do I get a PHP array instead of classes?
The tool always generates both. The second output box contains the same JSON converted to a nested PHP array literal with short [] syntax, quoted keys and proper escaping — handy for config files and test fixtures.
How are optional or null values handled?
They become nullable types like ?string, and fromArray() reads them with $data['key'] ?? null so missing keys hydrate to null instead of raising warnings.
Is my JSON processed on a server?
No. The conversion is plain JavaScript in your browser. Nothing is uploaded, logged or stored, so production payloads stay private.
Related tools
PHP Array to JSON Converter
Convert PHP array literals to JSON online. Parses array() and [] syntax, => keys, nesting and quotes, and outputs lists as real JSON arrays.
JSON to C# Converter
Convert JSON to C# classes or records online. Infers types from a sample, adds JsonPropertyName attributes and nullable types — all in your browser.
JSON to TypeScript
Generate TypeScript interfaces from a JSON sample online. Infers types recursively, merges array shapes and marks optional keys — 100% in your browser.
JSON Formatter
Format, beautify and minify JSON online with 2-space, 4-space or tab indentation. Sort keys alphabetically and catch syntax errors instantly — free and private.
JS Object to JSON Converter
Convert a JavaScript object literal to strict JSON online. Handles unquoted keys, single quotes, trailing commas and comments — without eval.