Search code examples
json-api

How to present empty relationship in JSONAPI?


Looked at http://jsonapi.org/format/ but don't see any description about empty relationship format, for example:

{
  "type": "articles",
  "id": "1",
  "attributes": {
    "title": "Rails is Omakase"
  },
  "relationships": {
    "comments": {
        "data": []
      }
  }
}

This article doesn't have comments, what is the correct way to present a empty relationship?

"data": [] or "data": null or no "relationships" at all?

Thanks!


Solution

  • It's described in resource linkage chapter of JSON API specification:

    Resource linkage MUST be represented as one of the following:

    • null for empty to-one relationships.
    • an empty array ([]) for empty to-many relationships.
    • a single resource identifier object for non-empty to-one relationships.
    • an array of resource identifier objects for non-empty to-many relationships.

    Note that you don't have to use resource linkage at all. You could also use relationship links. You could find more information about that one in this part of specification.