Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent collections in HAL (JSON version) while following REST

Let's say I have a collection of books which we'll call a library. GET domain.com/library should return a list of books in the library in HAL compliant JSON. How should I format the JSON? How could I embed the book resources? Here is the format I'm currently thinking about:

{
  "books": [
      {
          "name": "Fight Club",
          "_links": {
              "self": { 
                  "href": "domain.com/library/Fight-Club"
              },
           },
           ...
      },
       ....
  ],
  "_links" : {
       "search": { 
           "href": "domain.com/library/search"
       },
       ...
   },
   "_embedded" : {
       "Fight Club": {
           "author": "Chuck Palahniuk",
           ...
           [Same links as above]
       }
   }
}
like image 973
Mikayla Maki Avatar asked Aug 11 '14 19:08

Mikayla Maki


People also ask

What is HAL in rest?

The Hypertext Application Language (abbr. HAL) is an open specification that provides a structure to represent RESTful resources. It defines two hypermedia types to extend for XML and JSON.

What is a JSON collection?

A collection is a group of JSON documents that exist within one database. Documents within a collection can have different fields, although, generally all documents in a collection have a similar or related purpose.

What is Hateoas HAL?

HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API. The HAL format is strictly coupled to HATEOAS. The main target of HATEOAS is to decouple the API Consumer from the paths used in the API.

Is JSON hypertext?

Hypertext Application Language (HAL) is an Internet Draft (a "work in progress") standard convention for defining hypermedia such as links to external resources within JSON or XML code (however, the latest version of HAL Internet-Draft expired on November 12, 2016.).


1 Answers

As the HAL specification was written, the _embedded object is intended for sub-resources of a given resource. So your bare-bones HAL JSON would look like this.

{
    "_links": {
        "self": {
            "href": "/library"
        }
    },
    "_embedded": {
        "item": [{
            "_links": {
                "self": {
                    "href": "/library/Fight-Club"
                }
            },
            "author": "Chuck Palahniuk",
            "title":  "Fight Club"
        }]
    }
}

The immediate properties of the _embedded object are link relations. The item relation is a standard relation that means a resource is an item belonging to the context resource (in this case, your library). You can create custom link relation by using CURIEs.

Notice the lack of a books array in the top-level object. You may include it if you wish, but it's merely a convenience. A HAL library will only be aware of what you put in _links and _embedded. See this discussion regarding collections in HAL and the human factor in deciding where to put your data.

like image 85
Eric Avatar answered Oct 21 '22 08:10

Eric