PHPackages                             freddiegar/json-api-mapper - 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. [API Development](/categories/api)
4. /
5. freddiegar/json-api-mapper

ActiveLibrary[API Development](/categories/api)

freddiegar/json-api-mapper
==========================

Mapper response json-api in PHP

v1.0.0(8y ago)22011MITPHPPHP &gt;=7.1.3

Since May 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/freddiegar/json-api-mapper)[ Packagist](https://packagist.org/packages/freddiegar/json-api-mapper)[ RSS](/packages/freddiegar-json-api-mapper/feed)WikiDiscussions master Synced 2mo ago

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

JSON Api Mapper
===============

[](#json-api-mapper)

It is a mapper in PHP from response [jsonapi.org](http://jsonapi.org).

This library create a object from response json-api. Access to elements in response easily

Status Branch
-------------

[](#status-branch)

`master`: [![Build Status](https://camo.githubusercontent.com/57fb63a20ae373b830f320efa264c6aa52ddd16f493ea108332dd5a27e9231ac/68747470733a2f2f7472617669732d63692e6f72672f667265646469656761722f6a736f6e2d6170692d6d61707065722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/freddiegar/json-api-mapper)`develop`: [![Build Status](https://camo.githubusercontent.com/dbc9cbbb476e2e21b27029f8f77760fd00c73d672b3f55dcad0b3008c4c9e77e/68747470733a2f2f7472617669732d63692e6f72672f667265646469656761722f6a736f6e2d6170692d6d61707065722e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/freddiegar/json-api-mapper)`v1.0.0`: [![Build Status](https://camo.githubusercontent.com/efac3f0b96be585bf1219435a9eb0da7a96cc8b3f0e617d150bd17711c29e547/68747470733a2f2f7472617669732d63692e6f72672f667265646469656761722f6a736f6e2d6170692d6d61707065722e7376673f6272616e63683d76312e302e30)](https://travis-ci.org/freddiegar/json-api-mapper)

Requisites
----------

[](#requisites)

- php &gt;= 7.1.3

Install
-------

[](#install)

```
composer require freddiegar/json-api-mapper
```

Usage
-----

[](#usage)

Creating instance of Mapper, see $jsonApiResponse [here](#response-data)

```
use FreddieGar\JsonApiMapper\JsonApiMapper;

$jsonApi = new JsonApiMapper($jsonApiResponse);

$data = $jsonApi->getData(0);
$included = $jsonApi->getIncluded();
```

By example, get data resource

```
echo $data->getType(); // articles
echo $data->getId(); // 1

echo print_r($data->getAttributes(), true); // ['title' => 'JSON API paints my bikeshed!', 'body' => '...']
echo $data->getAttribute('created'); // 2015-05-22T14:56:29.000Z
echo $data->getAttribute('description'); // If not exist, return: null

echo print_r($data->getRelationships(), true); // ['author' => ['id' => '1', 'type' => 'people']]
echo get_class($data->getRelationship('author')); // return DataMapperInterface
echo $data->getRelationship('author')->getType(); // people
echo $data->getRelationship('author')->getId(); // 1
```

By example, get included

```
echo get_class($included->getIncluded(0)); // return DataMapperInterface
echo $included->getIncluded(0)->getType(); // people
echo $included->getIncluded(0)->getId(); // 42
echo $included->getIncluded(0)->getName(); // John
echo $included->getIncluded(1); // null, it is not defined in response
```

By example, get errors, see $jsonApiResponse [here](#response-errors)

```
$jsonApi = new JsonApiMapper($jsonApiResponse);

echo get_class($jsonApi->getErrors()); // Return ErrorsMapperInterface

$firstError = $jsonApi->getErrors(0); // Get first error

echo $firstError->getStatus(); // 422
echo print_r($firstError->getSource(), true); // ['pointer' => '/data/attributes/first-name']
echo $firstError->getTitle(); // Invalid Attribute
echo $firstError->getDetail(); // First name must contain at least three characters.

$secondError = $jsonApi->getErrors(1); // null, it is not defined in response
```

Find
----

[](#find)

### Get data with `id` = 2

[](#get-data-with-id--2)

```
$dataWithIdTwo = $data->find(2); // Return DataMapperInterface if exist else null
```

### Get included by `type` = people

[](#get-included-by-type--people)

```
$dataPeople = $included->find('people'); // Return DataMapperInterface if exist else null
```

### Get included with `type` = people and `id` = 3

[](#get-included-with-type--people-and-id--3)

```
$dataPeopleWithIdThree = $included->find('people', 3); // Return DataMapperInterface if exist else null
// OR
$peopleWithIdThree = $dataPeople->find(3); // Return DataMapperInterface if exist else null
```

Alias in JsonApiResponse class
------------------------------

[](#alias-in-jsonapiresponse-class)

You can use any option to access to data in that response

Method[\*](#performance)AliasPropertyDescriptiongetData()data()dataReturn object [DataMapper](https://github.com/freddiegar/json-api-mapper/blob/master/src/Contracts/DataMapperInterface.php) if exists in response, else nullgetErrors()errors()errorsReturn object [ErrorsMapper](https://github.com/freddiegar/json-api-mapper/blob/master/src/Contracts/ErrorsMapperInterface.php) if exists in response, else nullgetMeta()meta()metaReturn object [MetaMapper](https://github.com/freddiegar/json-api-mapper/blob/master/src/Contracts/MetaMapperInterface.php) if exists in response, else nullgetJsonApi()jsonapi()jsonapiReturn object [JsonApiMapper](https://github.com/freddiegar/json-api-mapper/blob/master/src/Contracts/ObjectJsonApiMapperInterface.php) if exists in response, else nullgetIncluded()included()includedReturn object [IncludedMapper](https://github.com/freddiegar/json-api-mapper/blob/master/src/Contracts/IncludedMapperInterface.php) if exists in response, else nullgetLinks()links()linksReturn object [LinksMapper](https://github.com/freddiegar/json-api-mapper/blob/master/src/Contracts/LinksMapperInterface.php) if exists in response, else nullPerformance
-----------

[](#performance)

You will prefer to use get\* (getData(), getErrors()) methods accessors, they are direct call, any other ways are overloading (`__call` and `__get`), this [are](https://gist.github.com/bwaidelich/7334680) **slower**

Response Used In Example
------------------------

[](#response-used-in-example)

You can see all example [here](https://github.com/freddiegar/json-api-mapper/blob/master/test/Mappers/ReadmeExample.php)

### Response [json-api](http://jsonapi.org/examples/#sparse-fieldsets) Resource used in this example

[](#response-json-api-resource-used-in-this-example)

```
{
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!",
      "body": "The shortest article. Ever.",
      "created": "2015-05-22T14:56:29.000Z",
      "updated": "2015-05-22T14:56:28.000Z"
    },
    "relationships": {
      "author": {
        "data": {"id": "42", "type": "people"}
      }
    }
  }],
  "included": [
    {
      "type": "people",
      "id": "42",
      "attributes": {
        "name": "John",
        "age": 80,
        "gender": "male"
      }
    }
  ]
}
```

### Response [json-api](http://jsonapi.org/examples/#error-objects-basics) Errors used in this example

[](#response-json-api-errors-used-in-this-example)

```
{
  "errors": [
      {
        "status": "422",
        "source": { "pointer": "/data/attributes/first-name" },
        "title":  "Invalid Attribute",
        "detail": "First name must contain at least three characters."
      }
    ]
}
```

License
-------

[](#license)

[MIT](https://opensource.org/licenses/MIT)

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

2932d ago

### Community

Maintainers

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

---

Tags

jsonapijsonapimapmappingmapperJSON-APIjsonapi.org

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/freddiegar-json-api-mapper/health.svg)

```
[![Health](https://phpackages.com/badges/freddiegar-json-api-mapper/health.svg)](https://phpackages.com/packages/freddiegar-json-api-mapper)
```

###  Alternatives

[neomerx/json-api

Framework agnostic JSON API (jsonapi.org) implementation

7373.6M27](/packages/neomerx-json-api)[cloudcreativity/laravel-json-api

JSON API (jsonapi.org) support for Laravel applications.

7881.1M5](/packages/cloudcreativity-laravel-json-api)[cloudcreativity/json-api-testing

PHPUnit test helpers to check JSON API documents.

141.6M3](/packages/cloudcreativity-json-api-testing)[alsvanzelf/jsonapi

Human-friendly library to implement JSON:API without needing to know the specification.

54150.0k5](/packages/alsvanzelf-jsonapi)[nilportugues/jsonapi-bundle

Symfony 2 &amp; 3 JSON API Transformer Package

11446.0k](/packages/nilportugues-jsonapi-bundle)[laravel-json-api/eloquent

Serialize Eloquent models as JSON:API resources.

121.4M4](/packages/laravel-json-api-eloquent)

PHPackages © 2026

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