PHPackages                             kwai/jsonapi - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. kwai/jsonapi

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

kwai/jsonapi
============

JSON:API serializer for PHP resources

1.0.2(4y ago)1301MITPHPPHP ^8.0

Since Nov 28Pushed 4y ago1 watchersCompare

[ Source](https://github.com/fbraem/kwai-jsonapi)[ Packagist](https://packagist.org/packages/kwai/jsonapi)[ RSS](/packages/kwai-jsonapi/feed)WikiDiscussions develop Synced 1w ago

READMEChangelog (3)Dependencies (1)Versions (5)Used By (0)

kwai-jsonapi
============

[](#kwai-jsonapi)

[![Latest Stable Version](https://camo.githubusercontent.com/e7ed331e43bc3bf6656ea8f776f15a999ecc153d582aca5ad9058f6a9d3eaf58/687474703a2f2f706f7365722e707567782e6f72672f6b7761692f6a736f6e6170692f76)](https://camo.githubusercontent.com/e7ed331e43bc3bf6656ea8f776f15a999ecc153d582aca5ad9058f6a9d3eaf58/687474703a2f2f706f7365722e707567782e6f72672f6b7761692f6a736f6e6170692f76)[![PHP Version Require](https://camo.githubusercontent.com/71b647087066d75a461a5e8579187799ac78af16ee61009b03d3c134eac0c0e6/687474703a2f2f706f7365722e707567782e6f72672f6b7761692f6a736f6e6170692f726571756972652f706870)](https://camo.githubusercontent.com/71b647087066d75a461a5e8579187799ac78af16ee61009b03d3c134eac0c0e6/687474703a2f2f706f7365722e707567782e6f72672f6b7761692f6a736f6e6170692f726571756972652f706870)

A JSON:API serializer for PHP classes using PHP attributes.

> Currently, this library has no support for [links](https://jsonapi.org/format/#document-links).

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

[](#installation)

```
composer require kwai/jsonapi

```

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

[](#requirements)

PHP attributes are used to serialize a PHP class to a JSONAPI resource. So, the PHP version must be at least 8.0. There are no other external dependencies.

Documentation
-------------

[](#documentation)

### \#\[JSONAPI/Resource\]

[](#jsonapiresource)

The "JSONAPI/Resource" attribute is used to set the type of the resource.

- The type argument is required.
- This attribute can only be applied to a class.
- By default, the id is retrieved from the id property. Use the id argument to use a method or a property with another name.

```
use Kwai\JSONAPI;

#[JSONAPI/Resource(type: 'people')]
class Person
{
    public function __construct(
        // We need at least an id property.
        private string $id,
    )
}
```

### \#\[JSONAPI/Attribute\]

[](#jsonapiattribute)

The "JSONAPI/Attribute" attribute is used to set an attribute of a resource.

- This attribute can be applied to a property or a method of a class.
- The name argument can be used to give a name to the property.
- When applied to a method, the name argument is required.

```
use Kwai\JSONAPI;

#[JSONAPI/Resource(type: 'people')]
class Person
{
    public function __construct(
        private string $id,
        #[JSONAPI/Attribute]
        private string $name,
        private int $age,
    ) {
    }

    #[JSONAPI/Attribute(name: 'age')]
    public function getAge(): int
    {
        return $this->age;
    }
}
```

Properties may be private. A method must be public.

With a Person instance like this:

```
$person = new Person(
    id: '1',
    name: 'Jigoro Kano',
    age: 77,
);
```

The result will be:

```
{
  "data": {
    "type": "people",
    "id": "1",
    "attributes": {
      "name": "Jigoro Kano",
      "age": 77
    }
  }
}
```

### \#\[JSONAPI/Relationship\]

[](#jsonapirelationship)

The "JSONAPI/Relationship" is used to map a relationship.

- This attribute can be applied to a property or a method.
- The name argument can be used to give a name to the relationship.
- When no name argument is set for a property, the name of the property will be used.
- When applied to a method, the name argument is required.

```
#[JSONAPI\Resource(type:'athletes')]
class Athlete
{
    public function __construct(
        private string $id,
        #[JSONAPI\Attribute]
        private string $name,
        #[JSONAPI\Relationship]
        private Country $country,
    ) {
    }
}
```

Properties may be private. A method must be public.

The linked resource must contain a JSONAPI\\Resource attribute. If not, a JSONAPI\\Exception will be thrown. A relationship can also be an array.

With the given PHP code:

```
$country = new Country(
    id: '1',
    code: 'BEL',
);
$athlete = new Athlete(
    id: '1',
    name: 'Ingrid Berghmans',
    country: $country
)
```

The result of the serializing will be:

```
{
  "data": {
    "type": "athletes",
    "id": "1",
    "attributes": {
      "name": "Ingrid Berghmans"
    },
    "relationships": {
      "country": {
        "data": {
          "type": "countries",
          "id": "1"
        }
      }
    }
  },
  "included": [
    {
      "type": "countries",
      "id": "1",
      "attributes": {
        "code": "BEL"
      }
    }
  ]
}
```

### Serializing

[](#serializing)

The JSONAPI\\Document class is used to serialize an object or an array to a JSON:API structure.

```
use Kwai\JSONAPI;

$person = new Person(
    id: '1',
    name: 'Jigoro Kano',
    age: 77,
);

try {
    $jsonapi = JSONAPI\Document::createFromObject($person)->serialize();
    // Send $jsonapi to the client...
} catch (JSONAPI\Exception $e) {
    // An exception occurred while serializing the PHP object.
}
```

### Meta

[](#meta)

Meta information can be set with the setMeta method.

```
    try {
        $jsonapi =
            JSONAPI\Document::createFromObject($person)
                ->setMeta('count', 1)
                ->serialize();
    } catch (JSONAPI\Exception $e) {
        // Handle exception...
    }
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.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 ~13 days

Total

3

Last Release

1603d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e8962d4432916692f9adad58507201d2a747c1cc294e97b86e49a9b650ac9468?d=identicon)[fbraem](/maintainers/fbraem)

---

Top Contributors

[![fbraem](https://avatars.githubusercontent.com/u/2566642?v=4)](https://github.com/fbraem "fbraem (16 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

json-apiphpjsonjsonapi

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/kwai-jsonapi/health.svg)

```
[![Health](https://phpackages.com/badges/kwai-jsonapi/health.svg)](https://phpackages.com/packages/kwai-jsonapi)
```

###  Alternatives

[justinrainbow/json-schema

A library to validate a json schema.

3.6k316.9M612](/packages/justinrainbow-json-schema)[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[colinodell/json5

UTF-8 compatible JSON5 parser for PHP

30422.2M45](/packages/colinodell-json5)[clue/ndjson-react

Streaming newline-delimited JSON (NDJSON) parser and encoder for ReactPHP.

15267.7M16](/packages/clue-ndjson-react)

PHPackages © 2026

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