Resource objects, attributes, and relationships
A JSON:API resource object has a type and an id, plus optional
attributes, relationships, links, and meta.
Attributes hold the resource's own data. Relationships describe connections to other resources.
{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
}
}
Notice that the author's name is not here. The relationship only says “this article's author is the
resource of type people with id 9”. That pair is called
resource linkage.
Resource linkage and relationship objects
A relationship object may contain links, data, and meta. The
data member is the linkage: a single resource identifier object for a to-one relationship,
an array for a to-many relationship, or null for an empty to-one relationship.
"relationships": {
"author": { "data": { "type": "people", "id": "9" } },
"comments": { "data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
] }
}
Resource identifier objects have a type and an id (or lid for
local identities in a create request). They do not carry attributes. To get the attributes, the client
must resolve the identifier against included.
Compound documents and included
A document that contains both primary data and related resources in included
is a compound document. This lets a server return an article, its author, and its
comments in one response without duplicating data inside each relationship.
{
"data": {
"type": "articles",
"id": "1",
"attributes": { "title": "JSON:API paints my bikeshed!" },
"relationships": {
"author": { "data": { "type": "people", "id": "9" } }
}
},
"included": [
{
"type": "people",
"id": "9",
"attributes": { "name": "Dan Gebhardt" },
"relationships": {
"employer": { "data": { "type": "companies", "id": "1" } }
}
}
]
}
Resolving the chain gives you articles #1 → author → people #9 → employer → companies #1. If
companies:1 is not present, the employer relationship is sparse.
The full linkage rule
JSON:API requires full linkage: every resource in included must be
reachable from the primary data through relationships, directly or indirectly. An included resource that
nothing points to is a specification violation. The validator flags these as unlinked.
The rule exists so clients can trust that included is exactly the set of related resources
needed to resolve the document — not an arbitrary dump of rows.
Sparse linkage and include
The include query parameter asks the server to return related resources in
included. It is usually a comma-separated path such as
include=author,comments.author. Servers may return only part of the requested graph, and a
relationship can point to a resource that is not included at all. This is called
sparse linkage and is allowed by the specification.
This is why a good viewer distinguishes three states: resolved (the object is present),
missing (linkage exists, object does not), and empty (data: null).
Cycles
Relationships can form cycles. A person manages a team, the team includes the person, and an article points to both. Recursive viewers must detect when a resource already appears on the current path and stop, or they will loop forever. JSON:API Lens shows a cycle marker with the path that closes the loop.
Inspect a real relationship graph
The fastest way to understand a document is to open it in the JSON:API viewer. Expand a relationship, follow it two or three hops, and watch the breadcrumbs and JSON pointers. The Graph tab shows the same structure as a node graph, with unresolved targets hidden by default.
Frequently asked questions
What is the difference between relationships and included in JSON:API?
A relationship holds resource linkage: the type and id of related resources. The
included array holds the full resource objects. A client resolves linkage against
included to get attributes.
What is full linkage in JSON:API?
Full linkage means every resource in included must be reachable from the primary data
through a chain of relationships. Included resources that nothing points to are a specification
violation.
Can a JSON:API relationship point to a missing resource?
Yes. Sparse linkage is allowed: a relationship may reference a resource that is not present in
data or included. The client can still show the identifier, and a viewer
should mark it as missing rather than dropping it.
Follow a relationship chain yourself
Open a document and expand it hop by hop.