Usage Guide

This guide provides an overview of how to use Ruby JSONAPI to serialize your Ruby objects according to the JSON:API specification.

Topics

Quick Example

Here’s a quick example of how to use Ruby JSONAPI:

# Define a serializer
class MovieSerializer
  include JSONAPI::Serializer

  attributes :name, :year, :rating

  has_many :actors
  belongs_to :director
end

# Serialize a single object
movie = Movie.find(1)
serializer = MovieSerializer.new(movie)
json_string = serializer.serializable_hash.to_json

# Serialize a collection
movies = Movie.all
serializer = MovieSerializer.new(movies)
json_string = serializer.serializable_hash.to_json

Output Format

The serialized output follows the JSON:API specification, which structures the data with type, id, and attributes:

{
  "data": {
    "id": "1",
    "type": "movie",
    "attributes": {
      "name": "Inception",
      "year": 2010,
      "rating": 8.8
    },
    "relationships": {
      "director": {
        "data": { "id": "3", "type": "director" }
      },
      "actors": {
        "data": [
          { "id": "5", "type": "actor" },
          { "id": "6", "type": "actor" }
        ]
      }
    }
  }
}

Choose a Topic

Select one of the topics listed above to dive deeper into using Ruby JSONAPI.


Table of contents