responsejson-apijsonapi-resources

What is the JSON:API standard of sending extra information about the resource?


I have an endpoint that responds with data to be filled in a sortable table. But some columns are not sortable columns and I want to communicate those columns in my response using JSON:API specifications.

I didn't find much information about these specifics.

So what is the best way to communicate this kind of data with the API consumer?

One opinion that I am inclined to, is to put them in the metadata field!

Many Thanks


Solution

  • JSON:API allows you to provide non-standard meta-information using a meta object. The meta information could be for the full document, per resource or per relationship of a resource. The information which fields are supported for sorting is a meta information for the full document.

    The specification does not cover how to format such meta informations. I'm also not aware of any recommendation or profile which covers your use case. So you should come up with your API specific implementation. E.g. you could provide an array of fields that could be used for sorting under sortable key of meta object. You may use dot-separated paths if you supporting sorting by fields of a relationship. This would be inline with the value of included query param.

    An example may look like this:

    {
      "data": [
        { 
          "type": "posts",
          "id": "1",
          "attributes": {
            "title": "An example post",
            "createdAt": "2019-08-05T11:11:11.000Z",
            "body": "..."
          },
          "relationships": {
            "author": {
              // ...
            },
          }
        },
        // ...
      },
      "meta": {
        "sortable": [
          "author.name",
          "createdAt",
          "title"
        ]
      }
    }
    

    Please note that I'm talking of fields. JSON:API specification does not have the term columns used in your question but I'm quite sure that you mean fields as defined by spec:

    A resource object’s attributes and its relationships are collectively called its “fields”.