{"id":17190330,"url":"https://github.com/lode/jsonapi","last_synced_at":"2026-01-05T23:12:51.692Z","repository":{"id":32721921,"uuid":"36311696","full_name":"lode/jsonapi","owner":"lode","description":"Human-friendly library to implement JSON:API without needing to know the specification.","archived":false,"fork":false,"pushed_at":"2022-12-09T14:58:38.000Z","size":693,"stargazers_count":52,"open_issues_count":7,"forks_count":9,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-09T21:36:33.549Z","etag":null,"topics":["api-server","json","json-api","jsonapi","php"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/alsvanzelf/jsonapi","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-26T17:19:44.000Z","updated_at":"2024-09-02T23:18:40.000Z","dependencies_parsed_at":"2023-01-14T22:02:06.009Z","dependency_job_id":null,"html_url":"https://github.com/lode/jsonapi","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lode%2Fjsonapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lode%2Fjsonapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lode%2Fjsonapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lode%2Fjsonapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lode","download_url":"https://codeload.github.com/lode/jsonapi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230046473,"owners_count":18164470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-server","json","json-api","jsonapi","php"],"created_at":"2024-10-15T01:14:00.769Z","updated_at":"2026-01-05T23:12:51.684Z","avatar_url":"https://github.com/lode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonapi\n\nA simple and human-friendly library for api servers (php serving json).\n\nIt allows you to generate json output according to the [JSON:API v1.1](https://jsonapi.org/) standard,\nwhile being easy to understand for people without knowledge of the jsonapi standard.\n\nThe JSON:API standard makes it easy for clients to fetch multiple resources in one call and understand the relations between them.\nRead more about it at [jsonapi.org](https://jsonapi.org/).\n\n\n## Installation\n\n[Use Composer](http://getcomposer.org/) require to get the latest stable version:\n\n```\ncomposer require alsvanzelf/jsonapi\n```\n\nThe library supports, and is is tested on, php versions 5.6, 7 and 8.\n\n#### Upgrading from v1\n\nIf you used v1 of this library, see [UPGRADE_1_TO_2.md](/UPGRADE_1_TO_2.md) on how to upgrade.\n\n\n\n## Getting started\n\n#### A small resource example\n\n```php\nuse alsvanzelf\\jsonapi\\ResourceDocument;\n\n$document = new ResourceDocument($type='user', $id=42);\n$document-\u003eadd('name', 'Zaphod Beeblebrox');\n$document-\u003eadd('heads', 2);\n$document-\u003esendResponse();\n```\n\nWhich will result in:\n\n```json\n{\n\t\"jsonapi\": {\n\t\t\"version\": \"1.1\"\n\t},\n\t\"data\": {\n\t\t\"type\": \"user\",\n\t\t\"id\": \"42\",\n\t\t\"attributes\": {\n\t\t\t\"name\": \"Zaphod Beeblebrox\",\n\t\t\t\"heads\": 2\n\t\t}\n\t}\n}\n```\n\n#### A collection of resources with relationships\n\n```php\nuse alsvanzelf\\jsonapi\\CollectionDocument;\nuse alsvanzelf\\jsonapi\\objects\\ResourceObject;\n\n$arthur      = new ResourceObject('user', 1);\n$ford        = new ResourceObject('user', 2);\n$zaphod      = new ResourceObject('user', 42);\n$heartOfGold = new ResourceObject('starship', 2001);\n\n$arthur-\u003eadd('name', 'Arthur Dent');\n$ford-\u003eadd('name', 'Ford Prefect');\n$zaphod-\u003eadd('name', 'Zaphod Beeblebrox');\n$heartOfGold-\u003eadd('name', 'Heart of Gold');\n\n$zaphod-\u003eaddRelationship('drives', $heartOfGold);\n\n$users    = [$arthur, $ford, $zaphod];\n$document = CollectionDocument::fromResources(...$users);\n$document-\u003esendResponse();\n```\n\nWhich will result in:\n\n```json\n{\n\t\"jsonapi\": {\n\t\t\"version\": \"1.1\"\n\t},\n\t\"data\": [\n\t\t{\n\t\t\t\"type\": \"user\",\n\t\t\t\"id\": \"1\",\n\t\t\t\"attributes\": {\n\t\t\t\t\"name\": \"Arthur Dent\"\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\t\"type\": \"user\",\n\t\t\t\"id\": \"2\",\n\t\t\t\"attributes\": {\n\t\t\t\t\"name\": \"Ford Prefect\"\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\t\"type\": \"user\",\n\t\t\t\"id\": \"42\",\n\t\t\t\"attributes\": {\n\t\t\t\t\"name\": \"Zaphod Beeblebrox\"\n\t\t\t},\n\t\t\t\"relationships\": {\n\t\t\t\t\"drives\": {\n\t\t\t\t\t\"data\": {\n\t\t\t\t\t\t\"type\": \"starship\",\n\t\t\t\t\t\t\"id\": \"2001\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t],\n\t\"included\": [\n\t\t{\n\t\t\t\"type\": \"starship\",\n\t\t\t\"id\": \"2001\",\n\t\t\t\"attributes\": {\n\t\t\t\t\"name\": \"Heart of Gold\"\n\t\t\t}\n\t\t}\n\t]\n}\n```\n\n#### Turning an exception into jsonapi\n\n```php\nuse alsvanzelf\\jsonapi\\ErrorsDocument;\n\n$exception = new Exception('That is not valid', 422);\n\n$document = ErrorsDocument::fromException($exception);\n$document-\u003esendResponse();\n```\n\nWhich will result in:\n\n```json\n{\n\t\"jsonapi\": {\n\t\t\"version\": \"1.1\"\n\t},\n\t\"errors\": [\n\t\t{\n\t\t\t\"status\": \"422\",\n\t\t\t\"code\": \"Exception\",\n\t\t\t\"meta\": {\n\t\t\t\t\"class\": \"Exception\",\n\t\t\t\t\"message\": \"That is not valid\",\n\t\t\t\t\"code\": 422,\n\t\t\t\t\"file\": \"README.md\",\n\t\t\t\t\"line\": 137,\n\t\t\t\t\"trace\": []\n\t\t\t}\n\t\t}\n\t]\n}\n```\n\nThis can be useful for development. For production usage, you can better construct an `ErrorsDocument` with only specific values.\n\n#### Using extensions and profiles\n\nThe [Atomic Operations extension](https://jsonapi.org/ext/atomic/) and the [Cursor Pagination profile](https://jsonapi.org/profiles/ethanresnick/cursor-pagination/) come packaged along. Any 3rd party of self-made extension can be applied with:\n\n```php\nuse alsvanzelf\\jsonapi\\ResourceDocument;\nuse alsvanzelf\\jsonapi\\interfaces\\ExtensionInterface;\n\nclass ExampleExtension implements ExtensionInterface {\n\tpublic function getOfficialLink() {\n\t\treturn 'https://example.org/extension-documentation';\n\t}\n\t\n\tpublic function getNamespace() {\n\t\treturn 'foo';\n\t}\n}\n\n$document = new ResourceDocument('user', 42);\n$document-\u003eadd('name', 'Zaphod Beeblebrox');\n\n$extension = new ExampleExtension();\n$document-\u003eapplyExtension($extension);\n$document-\u003eaddExtensionMember($extension, 'bar', 'baz');\n\n$document-\u003esendResponse();\n```\n\nWhich will result in:\n\n```json\n{\n    \"foo:bar\": \"baz\",\n    \"jsonapi\": {\n        \"version\": \"1.1\",\n        \"ext\": [\n            \"https://example.org/extension-documentation\"\n        ]\n    },\n    \"data\": {\n        \"type\": \"user\",\n        \"id\": \"42\",\n        \"attributes\": {\n            \"name\": \"Zaphod Beeblebrox\"\n        }\n    }\n}\n```\n\nA similar flow can be used for profiles.\n\n#### Other examples\n\nExamples for all kind of responses are in the [/examples](/examples) directory.\n\n\n## Features\n\nThis library supports [v1.1 of the JSON:API specification](https://jsonapi.org/format/1.1/).\n\nIt has support for generating \u0026 sending documents with:\n\n- single resources\n- resource collections\n- to-one and to-many relationships\n- errors (easily turning exceptions into jsonapi output)\n- v1.1 extensions and profiles\n- v1.1 @-members for JSON-LD and others\n\nAlso there's tools to help processing of incoming requests:\n\n- parse request options (include paths, sparse fieldsets, sort fields, pagination, filtering)\n- parse request documents for creating, updating and deleting resources and relationships\n\nNext to custom extensions/profiles, the following [official extensions/profiles](https://jsonapi.org/extensions/) are included:\n\n- Atomic Operations extension ([example code](/examples/atomic_operations_extension.php), [specification](https://jsonapi.org/ext/atomic/))\n- Cursor Pagination profile ([example code](/examples/cursor_pagination_profile.php), [specification](https://jsonapi.org/profiles/ethanresnick/cursor-pagination/))\n\nPlans for the future include:\n\n- validate request options ([#58](https://github.com/lode/jsonapi/issues/58))\n- validate request documents ([#57](https://github.com/lode/jsonapi/issues/57))\n\n\n## Contributing\n\nIf you use the library, please ask questions or share what can be improved by [creating an issue](https://github.com/lode/jsonapi/issues).\n\nFor bugs [issues](https://github.com/lode/jsonapi/issues) or [Pull Requests](https://github.com/lode/jsonapi/pulls) are welcome!\n\nSee [/script's README](/script) to steps to setup a local development environment.\n\n\n## Licence\n\n[MIT](/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flode%2Fjsonapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flode%2Fjsonapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flode%2Fjsonapi/lists"}