Exploring how to place navigational and other meta concerns in a JSON API.
Mandates that there will always be a "values" key in the collection hash.
// This is a collection of boats
"boats": {
"values": [ .. array of boat records .. ]
"meta": {
"self": "https://example.org/boats/page/1",
"next": "https://example.org/boats/page/2",
"prev": null,
"size": 109
}
}
// This is a boat record
"boat": {
"meta": {
"self": "https://example.org/boats/47",
"sailors": "https://example.org/boats/47/sailors"
}
"id": 47,
"name": "The Horse"
}
This breaks the symmetry in how collections and records surface "meta attributes"
"boats": [ .. array of boat records .. ],
"meta": {
"self": "https://example.org/boats?page=1",
"next": "https://example.org/boats?page=2",
"prev": null,
"size": 109
}
}
// This is a boat record
"boat": {
"meta": {
"self": "https://example.org/boats/47",
"sailors": "https://example.org/boats/47/sailors"
},
"id": 47,
"name": "The horse"
}
Adjust the boat record to align with how a collection sits next to the "meta" attributes. Navigating the response structure is becoming clunky at this point.
// This is a boat record
{
"boat": {
"id": 47,
"name": "The horse"
},
"meta": {
"self": "https://example.org/boats/47",
"sailors": "https://example.org/boats/47/sailors"
}
}
Reduce ambitions and just give records a "url" attribute an no other meta information. Maybe there won't be a second rising of the semantic web after all.
// This is a collection of boats
"boats": [ .. array of boat records .. ]
"next": "https://example.org/boats?page=2",
"prev": null,
"size": 109
// This is a boat record
"boat": {
"id": 47,
"url": "https://example.org/boats/47",
"name": "The Horse"
}
Resources don't know about meta. Use HTTP headers.
Collections:
Link: <https://example.org/boats?page=2>; rel="next", <https://example.org/boats?page=1>; rel="self"
X-Collection-Size: 109
Records:
Link: <https://example.org/boats/47/sailors>; rel="sailors", <https://example.org/boats/47>; rel="self"
If we can get away with it, I would greatly prefer to use headers. Adding complexity to our resource representations sucks.
Also: we should always add URLs directly in the representations of our resources, e.g. each object has a
url
attribute. We could also add URLs for resources, e.g.comments_url
.