I want to load related records inside "relationships" key

Hi! I'm following the Ash book, specifically on loading related records:
json_api do
type "artist"
includes [:albums]
end
json_api do
type "artist"
includes [:albums]
end
Where I wanted to return the related records (:albums) of :artist resource and the attributes of :albums are already public. It returns:
{
"data": [
{
"attributes": {
"name": "Resonanz",
"inserted_at": "2025-05-14T10:04:02.255547Z",
"previous_names": [],
"updated_at": "2025-05-14T10:04:02.255547Z"
},
"id": "030a340b-2d9d-4961-ae5a-5c7b80a30215",
"links": {},
"meta": {},
"type": "artist",
"relationships": {
"albums": {
"data": [
{
"id": "272bb477-3416-42e1-bcb4-551f11d26ee8",
"type": "album"
<For some reason the attributes are public, but why I can't see it?>
}
],
"links": {},
"meta": {}
}
}
}
]
}
{
"data": [
{
"attributes": {
"name": "Resonanz",
"inserted_at": "2025-05-14T10:04:02.255547Z",
"previous_names": [],
"updated_at": "2025-05-14T10:04:02.255547Z"
},
"id": "030a340b-2d9d-4961-ae5a-5c7b80a30215",
"links": {},
"meta": {},
"type": "artist",
"relationships": {
"albums": {
"data": [
{
"id": "272bb477-3416-42e1-bcb4-551f11d26ee8",
"type": "album"
<For some reason the attributes are public, but why I can't see it?>
}
],
"links": {},
"meta": {}
}
}
}
]
}
I understand that the related records go in "included": ... key. But what I really wanted is to show the related attributes inside "relationships" key:
{
"data": [
{
"attributes": {
...
},
"id": "030a340b-2d9d-4961-ae5a-5c7b80a30215",
"type": "artist",
"relationships": {
"albums": {
"data": [
{
"id": "272bb477-3416-42e1-bcb4-551f11d26ee8",
"type": "album"
... <-- I want to see the attributes! :(
},
],
"links": {},
"meta": {}
}
}
}
]
}
{
"data": [
{
"attributes": {
...
},
"id": "030a340b-2d9d-4961-ae5a-5c7b80a30215",
"type": "artist",
"relationships": {
"albums": {
"data": [
{
"id": "272bb477-3416-42e1-bcb4-551f11d26ee8",
"type": "album"
... <-- I want to see the attributes! :(
},
],
"links": {},
"meta": {}
}
}
}
]
}
5 Replies
sevenseacat
sevenseacat3w ago
is that valid according to the JSON:API spec?
Jaeyson Anthony Y.
@sevenseacat yes, a valid spec. I trimmed some data in response to make it shorter to read. The ... and <some comments i made> were just me adding comments in this thread, not part of the original api response.
sevenseacat
sevenseacat3w ago
reading through https://jsonapi.org/format/#document-compound-documents and it mentions that includes should be done with resource identifier objects in relationships, and the actual records in included, which is what we have now
Jaeyson Anthony Y.
Got it. But I'm curious how do I load the public attributes (:album) in relationships? My understanding from this is: match the album id in relationships (since the data only contains id and type) to what's inside included
sevenseacat
sevenseacat3w ago
yep yep

Did you find this page helpful?