Returning null values in responses


#1

I couldn’t find any statement or recommendation in the spec regarding null values in document responses.

E.g. take this example from the spec:

{
  "links": {
    "self": "http://example.com/articles/1/author"
  },
  "data": null
}

The data member is null here, which is required as the top level document must provide one of data, errors or meta.

When we look at the attributes the spec e.g. contains:

"attributes": {
  "title": "Rails is Omakase"
}

but what if the resource would contain a attribute with a null value?

I always like to explicitly return it like:

"attributes": {
  "title": "Rails is Omakase",
  "subtitle": null
}

It might be a arbitrary question but I wonder how others do this.

Note when e.g. returning a document without meta, links or jsonapi oject my preference is not to include them in the document:

{
  "data": [ .. ] 
  "meta": null,
  "links": null,
  "jsonapi": null
}

So basically I prefer to only return null values for the data member (as it might be required) or for fields within the attributes member.