JSON To PHP Generator

Need to convert PHP into JSON Schema?

PHP → JSON Schema

JSON Input

Loading editor…

Generated PHP

Configuration

PHP Generation Options

Enter JSON data to generate PHP code

Classes with getters and setters

How to convert JSON to PHP – step-by-step guide

Use this JSON to PHP generator to create PHP classes from JSON samples for Laravel, Symfony, and backend integrations.

  1. Step 1 – Paste a JSON sample

    • Paste a representative JSON object or array into the left editor.
    • Include nested objects, arrays, and nullable fields so types are inferred correctly.
    • Use Import to load JSON from a file, URL, or sample data.
  2. Step 2 – Choose PHP class options

    • Set a root Class Name and namespace if you use PSR-4 autoloading.
    • Decide how to represent optional fields (nullable types like ?string).
    • Review nested objects and arrays so they map to dedicated classes and typed arrays where possible.
  3. Step 3 – Review the generated code

    • Check field names, types, and how arrays/objects are modeled.
    • Adjust options like Root Type Name, null handling, and frameworks if available.
    • If a field is inferred incorrectly, tweak your sample JSON and regenerate.
  4. Step 4 – Use the classes in PHP

    • Copy the classes into your project and ensure autoloading is configured.
    • Map decoded JSON arrays into your DTOs (or use a hydrator).
    • Validate required fields before using the data in business logic.
  5. Step 5 – Copy or download

    • Copy the output into your project or download it as a file.
    • Run your formatter/linter to match your code style.
    • Add JSON parsing/serialization libraries if your language requires them.

Quick tips

  • Prefer typed properties (PHP 7.4+) for clearer contracts.
  • Keep DTOs separate from ORM models to avoid coupling.
  • If you accept external JSON, validate with a JSON Schema first.
Example output (simplified)
// JSON input
{
  "id": 123,
  "name": "Maeve Winters",
  "email": "maeve@example.com",
  "active": true,
  "roles": ["admin", "editor"],
  "metadata": { "plan": "pro" },
  "createdAt": "2024-03-01T10:15:00Z",
  "score": 99.5,
  "notes": null
}

// Generated PHP DTOs (simplified)
final class Metadata {
  public string $plan;
}

final class Root {
  public int $id;
  public string $name;
  public ?string $email;
  public bool $active;
  /** @var string[] */
  public array $roles;
  public Metadata $metadata;
  public string $createdAt;
  public float $score;
  public mixed $notes;
}

Related JSON & PHP tools

Explore more JSON and schema tools that work great alongside this JSON to PHP generator.

Frequently Asked Questions

What PHP features are supported?

The generator creates PHP classes with type hints (PHP 7.4+), getters/setters, constructors, and supports various frameworks like Laravel Eloquent models and plain PHP classes.

Can I generate Laravel Eloquent models?

Yes! Select 'Laravel' as the framework to generate Eloquent model classes that extend the base Model class with proper imports and Laravel conventions.

How are PHP namespaces handled?

You can specify a custom namespace in the options. The generator will add the appropriate namespace declaration at the top of the generated PHP file.

What about PHP type hints and strict typing?

The generator uses modern PHP type hints for method parameters and return types, supporting bool, int, float, string, and array types based on your JSON data.

JSON to PHP Class Generator Online | JSONSwiss