PHPackages                             context-hub/json-schema-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. context-hub/json-schema-generator

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

context-hub/json-schema-generator
=================================

Provides the ability to generate JSON schemas from Data Transfer Object (DTO) classes

2.x-dev(1y ago)14[1 PRs](https://github.com/context-hub/json-schema-generator/pulls)MITPHPPHP &gt;=8.3

Since Apr 19Pushed 1y agoCompare

[ Source](https://github.com/context-hub/json-schema-generator)[ Packagist](https://packagist.org/packages/context-hub/json-schema-generator)[ Docs](https://github.com/spiral/json-schema-generator)[ RSS](/packages/context-hub-json-schema-generator/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelogDependencies (7)Versions (3)Used By (0)

JSON Schema Generator
=====================

[](#json-schema-generator)

[![PHP Version Require](https://camo.githubusercontent.com/940acf4fb73d736f4029a6f637c677325d2f49b06d56815d932147bfdc6a9496/68747470733a2f2f706f7365722e707567782e6f72672f636f6e746578742d6875622f6a736f6e2d736368656d612d67656e657261746f722f726571756972652f706870)](https://packagist.org/packages/context-hub/json-schema-generator)[![Latest Stable Version](https://camo.githubusercontent.com/f639f18a326d94a20dc0b7c35841b6620ca063073e85e7c6f9035cbae1045d89/68747470733a2f2f706f7365722e707567782e6f72672f636f6e746578742d6875622f6a736f6e2d736368656d612d67656e657261746f722f762f737461626c65)](https://packagist.org/packages/context-hub/json-schema-generator)[![phpunit](https://github.com/context-hub/json-schema-generator/actions/workflows/phpunit.yml/badge.svg)](https://github.com/context-hub/json-schema-generator/actions)[![psalm](https://github.com/context-hub/json-schema-generator/actions/workflows/psalm.yml/badge.svg)](https://github.com/context-hub/json-schema-generator/actions)[![Total Downloads](https://camo.githubusercontent.com/7489b3be8e7e81fad8b1a8f14dcc5174a1aad1a32796a6f036e48767a88824c6/68747470733a2f2f706f7365722e707567782e6f72672f636f6e746578742d6875622f6a736f6e2d736368656d612d67656e657261746f722f646f776e6c6f616473)](https://packagist.org/packages/context-hub/json-schema-generator)[![psalm-level](https://camo.githubusercontent.com/0fad001f36bde6a9d8ab41bcda4c043378972315d2385fc409c146eab337773a/68747470733a2f2f73686570686572642e6465762f6769746875622f636f6e746578742d6875622f6a736f6e2d736368656d612d67656e657261746f722f6c6576656c2e737667)](https://shepherd.dev/github/context-hub/json-schema-generator)

The JSON Schema Generator is a PHP package that simplifies the generation of [JSON schemas](https://json-schema.org/) from Data Transfer Object (DTO) classes. It supports PHP enumerations and generic type annotations for arrays and provides an attribute for specifying title, description, and default value.

Main use case - structured output definition for LLMs.

Requirements
------------

[](#requirements)

Make sure that your server is configured with the following PHP versions and extensions:

- PHP &gt;=8.1

Installation
------------

[](#installation)

You can install the package via Composer:

```
composer require context-hub/json-schema-generator
```

Usage
-----

[](#usage)

To generate a schema for a DTO, instantiate the `Spiral\JsonSchemaGenerator\Generator` and call the **generate** method, passing the DTO class as an argument (fully qualified class name or reflection). The method will return an instance of `Spiral\JsonSchemaGenerator\Schema`.

Let's create a simple data transfer object:

```
namespace App\DTO;

use Spiral\JsonSchemaGenerator\Attribute\Field;

class Movie
{
    public function __construct(
        #[Field(title: 'Title', description: 'The title of the movie')]
        public readonly string $title,
        #[Field(title: 'Year', description: 'The year of the movie')]
        public readonly int $year,
        #[Field(title: 'Description', description: 'The description of the movie')]
        public readonly ?string $description = null,
        public readonly ?string $director = null,
        #[Field(title: 'Release Status', description: 'The release status of the movie')]
        public readonly ?ReleaseStatus $releaseStatus = null,
    ) {
    }
}
```

This DTO has a **releaseStatus**, which is an enum. Let's create it:

```
namespace App\DTO;

enum ReleaseStatus: string
{
    case Released = 'Released';
    case Rumored = 'Rumored';
    case PostProduction = 'Post Production';
    case InProduction = 'In Production';
    case Planned = 'Planned';
    case Canceled = 'Canceled';
}
```

Now, let's generate a schema for this DTO:

```
use Spiral\JsonSchemaGenerator\Generator;
use App\DTO\Movie;

$generator = new Generator();
$schema = $generator->generate(Movie::class);
```

> **Note**Additionally, the package provides the `Spiral\JsonSchemaGenerator\GeneratorInterface,` which can be integrated into your application's dependency container for further customization and flexibility.

The `Spiral\JsonSchemaGenerator\Schema` object implements the **JsonSerializable** interface, allowing easy conversion of the schema into either JSON or a PHP array.

Example array output:

```
[
    'properties'  => [
        'title'         => [
            'title'       => 'Title',
            'description' => 'The title of the movie',
            'type'        => 'string',
        ],
        'year'          => [
            'title'       => 'Year',
            'description' => 'The year of the movie',
            'type'        => 'integer',
        ],
        'description'   => [
            'title'       => 'Description',
            'description' => 'The description of the movie',
            'type'        => 'string',
        ],
        'director'      => [
            'type' => 'string',
        ],
        'releaseStatus' => [
            'title'       => 'Release Status',
            'description' => 'The release status of the movie',
            'allOf'       => [
                [
                    '$ref' => '#/definitions/ReleaseStatus',
                ],
            ],
        ],
    ],
    'required'    => [
        'title',
        'year',
    ],
    'definitions' => [
        'ReleaseStatus' => [
            'title' => 'ReleaseStatus',
            'type'  => 'string',
            'enum'  => [
                'Released',
                'Rumored',
                'Post Production',
                'In Production',
                'Planned',
                'Canceled',
            ],
        ],
    ],
];
```

Class properties can be arrays, and the type of elements within the array can be specified using PHPDoc annotations.

For example, we have a DTO with an array of objects:

```
namespace App\DTO;

use Spiral\JsonSchemaGenerator\Attribute\Field;

final class Actor
{
    public function __construct(
        public readonly string $name,
        /**
         * @var array
         */
        public readonly array $movies = [],
    ) {
    }
}
```

In this example, we use a PHPDoc block to indicate that the property **$movies** contains an array of **Movie** objects.

> **Note**Various documentation type annotations are supported, including `@var array`, `@var Movie[]`, and `@var list`. For promoted properties, you can use annotations like `@param array $movies`, `@param Movie[] $movies`, and `@param list $movies`.

Now, let's generate a schema for this DTO:

```
use Spiral\JsonSchemaGenerator\Generator;
use App\DTO\Actor;

$generator = new Generator();
$schema = $generator->generate(Actor::class);
```

Example array output:

```
[
    'properties' => [
        'name'   => [
            'type' => 'string',
        ],
        'movies' => [
            'type'  => 'array',
            'items' => [
                '$ref' => '#/definitions/Movie',
            ],
            'default' => [],
        ],
    ],
    'required'   => [
        'name',
    ],
    'definitions' => [
        'Movie'         => [
            'title'      => 'Movie',
            'type'       => 'object',
            'properties' => [
                'title'         => [
                    'title'       => 'Title',
                    'description' => 'The title of the movie',
                    'type'        => 'string',
                ],
                'year'          => [
                    'title'       => 'Year',
                    'description' => 'The year of the movie',
                    'type'        => 'integer',
                ],
                'description'   => [
                    'title'       => 'Description',
                    'description' => 'The description of the movie',
                    'type'        => 'string',
                ],
                'director'      => [
                    'type' => 'string',
                ],
                'releaseStatus' => [
                    'title'       => 'Release Status',
                    'description' => 'The release status of the movie',
                    'allOf'       => [
                        [
                            '$ref' => '#/definitions/ReleaseStatus',
                        ],
                    ],
                ],
            ],
            'required'   => [
                'title',
                'year',
            ],
        ],
        'ReleaseStatus' => [
            'title' => 'ReleaseStatus',
            'type'  => 'string',
            'enum'  => [
                'Released',
                'Rumored',
                'Post Production',
                'In Production',
                'Planned',
                'Canceled',
            ],
       ]
    ],
];
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance47

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 57.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

395d ago

Major Versions

1.x-dev → 2.x-dev2025-04-19

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/773481?v=4)[Pavel Buchnev](/maintainers/butschster)[@butschster](https://github.com/butschster)

---

Top Contributors

[![msmakouz](https://avatars.githubusercontent.com/u/67324318?v=4)](https://github.com/msmakouz "msmakouz (12 commits)")[![wolfy-j](https://avatars.githubusercontent.com/u/796136?v=4)](https://github.com/wolfy-j "wolfy-j (5 commits)")[![butschster](https://avatars.githubusercontent.com/u/773481?v=4)](https://github.com/butschster "butschster (3 commits)")[![raphaelstolt](https://avatars.githubusercontent.com/u/48225?v=4)](https://github.com/raphaelstolt "raphaelstolt (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Type Coverage Yes

### Embed Badge

![Health badge](/badges/context-hub-json-schema-generator/health.svg)

```
[![Health](https://phpackages.com/badges/context-hub-json-schema-generator/health.svg)](https://phpackages.com/packages/context-hub-json-schema-generator)
```

###  Alternatives

[phpdocumentor/reflection-docblock

With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.

9.4k722.2M1.2k](/packages/phpdocumentor-reflection-docblock)[symfony/serializer-pack

A pack for the Symfony serializer

1.1k28.2M221](/packages/symfony-serializer-pack)[symfony/ai-platform

PHP library for interacting with AI platform provider.

51927.7k136](/packages/symfony-ai-platform)[symfony/ai-agent

PHP library for building agentic applications.

30536.7k44](/packages/symfony-ai-agent)[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

310107.9k1](/packages/cognesy-instructor-php)[spomky-labs/pwa-bundle

Progressive Web App Manifest Generator Bundle for Symfony.

6144.4k1](/packages/spomky-labs-pwa-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
