Skip to main content
Convert2JSON Tools

JSON to PHP Converter

Generate modern, typed PHP classes from a JSON sample — or a plain array literal when a class would be overkill.

Your data stays on your device

Input — JSON

characters
0
lines
0
size
0 B

Output — PHP

characters
0
lines
0
size
0 B

Options

About this tool

PHP 8 added typed properties, constructor property promotion and readonly modifiers, which together make data classes genuinely concise. Generated models can now express the shape of an API response without a hundred lines of boilerplate.

The alternative output is a PHP array literal, which is often what you actually want for a fixture, a config file or a quick test — a direct transcription of the JSON into PHP syntax.

How to use this tool

  1. Paste your JSON sample.
  2. Choose class output or array literal output.
  3. Set the root class name and choose whether properties should be readonly.
  4. Press Generate and copy the code.

Key features

  • Typed properties with constructor property promotion
  • Optional readonly modifiers for immutable value objects
  • Nullable types marked with ? where the sample allows null
  • Nested objects generated as separate classes
  • array literal output as an alternative to classes
  • PHP reserved words handled so the output always parses

Example

Class with promoted properties

InputJSON

{"id":4,"label":"beta"}

OutputPHP

final class Root
{
    public function __construct(
        public readonly int $id,
        public readonly string $label,
    ) {
    }
}

Good to know

  • PHP arrays are untyped, so array properties are typed simply as array with a docblock noting the element type.
  • No namespace or strict_types declaration is emitted; add them to match your project.
  • Constructor promotion requires PHP 8.0 or later, and readonly requires 8.1.

Frequently asked questions

Class or array literal?

Classes when the data is part of your domain and you want type checking. Array literals for fixtures, seed data and configuration where a class adds nothing.

What is constructor property promotion?

A PHP 8 feature that declares and assigns a property directly in the constructor signature, removing the usual duplicated declaration and assignment.

Can I type array elements?

Not in the PHP type system. The generator adds a docblock such as @var string[] so static analysers and your editor still understand the element type.

Should I make properties readonly?

Yes for DTOs that should not change after construction. It prevents accidental mutation and makes the object safe to pass around.

Your data stays on your device

Your data is processed locally in your browser and is not uploaded to our server. This page keeps working after you go offline, because there is nothing to send.

Share this toolXLinkedInHacker News