JSON:API is a specification for building APIs that use JSON. It's not just another way to format JSON—it's a complete standard that defines how clients and servers should communicate, how to structure requests and responses, and how to handle relationships, pagination, filtering, and more.
Think of JSON:API as a set of rules that makes APIs more consistent, predictable, and easier to work with. Instead of every API having its own unique structure, JSON:API provides a common pattern that developers can learn once and use everywhere. For practical examples of working with APIs, see our Working with JSON APIs guide.
Key Point: JSON:API is a specification, not a library or framework. It defines the rules for how JSON should be structured in API communication. Learn more at jsonapi.org.
Why JSON:API Was Created
Before JSON:API, every REST API had its own unique way of structuring JSON responses. This led to several problems:
JSON:API solves these problems by providing a standard that's detailed enough to be useful but flexible enough for most use cases. Learn more about JSON:API format specification.
Basic JSON:API Response Structure
Every JSON:API response follows a consistent structure. Here's a simple example:
Simple Resource Response
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API Tutorial",
"content": "Learn about JSON:API...",
"published": true
}
}
}Key elements: data contains the resource,type identifies the resource type,id is the unique identifier,attributes holds the actual data.
Relationships in JSON:API
One of JSON:API's most powerful features is how it handles relationships between resources. You can include related data or just reference it.
Resource with Relationships
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "Understanding JSON:API"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
},
"comments": {
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
}
}
}The relationships key defines connections to other resources.
Including Related Resources (Compound Documents)
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "Understanding JSON:API"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
}
},
"included": [
{
"type": "people",
"id": "9",
"attributes": {
"name": "Dan Gebhardt",
"twitter": "@dgeb"
}
}
]
}The included array contains related resources to avoid additional requests.
Collections and Arrays
When returning multiple resources, the data key contains an array:
Collection Response
{
"data": [
{
"type": "articles",
"id": "1",
"attributes": {
"title": "First Article"
}
},
{
"type": "articles",
"id": "2",
"attributes": {
"title": "Second Article"
}
}
],
"meta": {
"total": 42
}
}The meta object can contain additional information like total count.
Pagination in JSON:API
JSON:API provides standard links for pagination:
Paginated Response
{
"data": [
// ... array of resources
],
"links": {
"self": "https://api.example.com/articles?page[number]=2",
"first": "https://api.example.com/articles?page[number]=1",
"prev": "https://api.example.com/articles?page[number]=1",
"next": "https://api.example.com/articles?page[number]=3",
"last": "https://api.example.com/articles?page[number]=10"
},
"meta": {
"total-pages": 10,
"total-items": 200
}
}Clients can easily navigate through pages using the provided links.
Error Responses
JSON:API has a standard format for errors with detailed information:
Error Response Example
{
"errors": [
{
"status": "422",
"title": "Invalid Attribute",
"detail": "Title must be at least 3 characters long",
"source": {
"pointer": "/data/attributes/title"
}
},
{
"status": "422",
"title": "Invalid Attribute",
"detail": "Email has already been taken",
"source": {
"pointer": "/data/attributes/email"
}
}
]
}Each error includes status, title, detail, and can point to the specific field that caused it.
Key Features of JSON:API
1. Sparse Fieldsets
Request only the fields you need:
GET /articles?fields[articles]=title,body2. Filtering
Filter resources with query parameters:
GET /articles?filter[published]=true3. Sorting
Sort by one or more fields:
GET /articles?sort=-created,titleThe minus sign (-) indicates descending order.
4. Including Related Resources
Fetch related data in a single request:
GET /articles/1?include=author,commentsWhen to Use JSON:API
✅ Good Use Cases
- •Complex data with many relationships
- •Public APIs used by many clients
- •Need for consistency across endpoints
- •Building reusable API clients
- •Need advanced filtering/pagination
❌ May Not Be Ideal For
- •Very simple APIs with flat data
- •Microservices with unique needs
- •Performance-critical scenarios (overhead)
- •When team isn't familiar with spec
- •Real-time streaming data
JSON:API vs Traditional REST APIs
| Aspect | Traditional REST | JSON:API |
|---|---|---|
| Structure | Varies by API | Standardized format |
| Relationships | Nested or separate requests | Built-in with include |
| Pagination | Custom implementation | Standard links pattern |
| Errors | Varies widely | Structured error objects |
| Flexibility | Completely flexible | Structured but extensible |
| Learning Curve | Learn each API | Learn once, use everywhere |
JSON:API Libraries and Tools
Many libraries exist to help you implement JSON:API in various languages:
JavaScript/Node.js
Ruby
PHP
Find more implementations at jsonapi.org/implementations.
Helpful Tools for Working with JSON:API
Learn More About JSON:API
Summary
JSON:API is a powerful specification for building consistent, predictable APIs with JSON. It solves common problems like relationship handling, pagination, and error formatting by providing a standard structure.
- •Standardized structure for requests and responses
- •Built-in support for relationships and compound documents
- •Consistent patterns for pagination, filtering, and sorting
- •Structured error handling with detailed information
- •Extensive ecosystem of libraries and tools
Next Steps: Ready to build or consume JSON APIs? Check our practical guide to working with JSON APIs with code examples in multiple languages.