Xerobit

JSON to PHP Class Converter

Paste a JSON object to generate a PHP class with typed public properties and a constructor that maps each key. Nested objects produce separate classes automatically. The json to php class conversion runs entirely in your browser, so nothing is uploaded to any server.

PHP Code

<?php

class Root {
    public int $id;
    public string $name;
    public bool $active;

    public function __construct(
        int $id,
        string $name,
        bool $active
    ) {
        $this->id = $id;
        $this->name = $name;
        $this->active = $active;
    }
}

How to use the json to php class converter

  1. Paste your JSON object into the JSON input text area.
  2. Change the Root class name if you want something other than Root.
  3. The PHP code updates live as you type or paste.
  4. Copy the generated classes into your PHP project.

How the PHP class is generated

The converter parses the JSON string using the built-in JSON.parse function available in every modern browser. It expects the root value to be an object rather than an array or primitive. For every key in that object, it determines the appropriate PHP type: strings become string, whole numbers become int, decimals become float, booleans stay bool, arrays become array, and null becomes mixed.

If a value is itself a nested object, the tool generates a separate class for it and uses that new class name as the property type in the parent. Keys are converted to PascalCase for class names and camelCase for property names. Invalid identifier characters such as hyphens or spaces are stripped during this transformation.

For example, given the input {"user_id": 42, "is_active": true} with the root class name User, the output declares public int $userId; and public bool $isActive;. The constructor accepts both parameters and assigns them to the corresponding properties.

Type mapping reference

This table shows how each JSON type translates to a PHP type declaration in the generated class. Understanding these mappings helps when working with the json to php class converter on complex payloads.

JSON typePHP typeExample value
Stringstring"hello"
Integerint42
Floatfloat3.14
Booleanbooltrue
Nullmixednull
Arrayarray[1, 2]
ObjectClass name{"a":1}

When a property can hold multiple types at runtime, PHP developers often use union types or docblocks. This tool keeps things simple by assigning a single type based on the first value encountered in the JSON structure.

Handling nested objects and arrays

When the converter encounters a nested object, it creates a new class definition below the parent. For example, a user object containing an address object produces both a User class and an Address class. The $address property inside User is typed as Address, making the relationship explicit in your codebase.

Arrays are always typed as array. If an array contains objects, those objects do not generate separate classes because PHP arrays are untyped by default. This keeps the output simple and avoids guessing element types from potentially empty or mixed arrays. If you need strict typing for collections, consider wrapping the array elements in a dedicated class manually after generation.

Duplicate class names are resolved automatically. If two different keys would produce the same PascalCase class name, the second one receives a numeric suffix to prevent collisions in the final PHP output.

Comparison with other language generators

PHP classes use public properties and a constructor that accepts parameters matching each property. This differs from languages like Java or C# where private fields with getters and setters are more common. The json to php class approach favors simplicity and direct assignment over encapsulation boilerplate.

If you need to convert the same JSON to other targets, several sibling tools follow similar parsing logic but emit idiomatic syntax for their respective languages. The json to java class converter generates POJOs with getters and setters. The json to c# class converter produces properties with attributes. The json to go struct converter outputs structs with JSON tags, and the json to python class converter creates dataclasses or standard classes depending on configuration.

Common edge cases

A few situations require attention when converting JSON to a PHP class:

  • Empty objects ({}) produce a class with no properties and an empty constructor.
  • Keys with spaces or hyphens are converted to valid PHP identifiers by removing non-alphanumeric characters and applying camelCase. A key like first-name becomes $firstName.
  • Unicode characters in string values are preserved exactly as they appear in the JSON source.
  • Large integers beyond JavaScript safe integer limits may lose precision during parsing. This is a limitation of the browser environment rather than the converter itself.
  • Numeric keys are prefixed with an underscore since PHP property names cannot start with a digit.

Worked examples

ExampleInputResult
Simple userJSON input: {"id":1,"name":"Alice","active":true}; Root class name: User<?php / class User { / public int $id; / public string $name; / public bool $active; / public function __construct( / int $id, / string $name, / bool $active / ) { / $this->id = $id; / $this->name = $name; / $this->active = $active; / } / } /
Nested addressJSON input: {"name":"Bob","address":{"street":"123 Main St","city":"Springfield"}}; Root class name: Person<?php / class Person { / public string $name; / public Address $address; / public function __construct( / string $name, / Address $address / ) { / $this->name = $name; / $this->address = $address; / } / } / class Address { / public string $street; / public string $city; / public function __construct( / string $street, / string $city / ) { / $this->street = $street; / $this->city = $city; / } / } /
Array of tagsJSON input: {"title":"Post","tags":["php","json"]}; Root class name: Article<?php / class Article { / public string $title; / public array $tags; / public function __construct( / string $title, / array $tags / ) { / $this->title = $title; / $this->tags = $tags; / } / } /

FAQ

Does this tool upload my JSON anywhere?

No. The json to php class conversion happens entirely in your browser using JavaScript. Nothing is sent to any server.

Can I convert a JSON array at the root level?

No. The converter expects a single JSON object at the root so it can generate a named class. Wrap your array in an object if needed.

What PHP version does the output target?

The generated classes use typed properties compatible with PHP 7.4 and later. The mixed type requires PHP 8.0 or newer.

How are null values handled?

Properties with a null value in the JSON are typed as mixed. No default value is assigned so the constructor parameter remains required.

Are nested objects supported?

Yes. Each nested object generates its own class, and the parent property uses that class name as its type.

Can I change the root class name?

Yes. Use the Root class name field to specify any valid PHP class name before copying the output.