Customizing JSON Serialization

Learn how to serialize the JSON in Rails application.

We'll cover the following

Optimizing the JSON

The as_json method takes an optional argument called options. Every object in our Rails app will respect two options passed to as_json, which are mutually exclusive:

  • :except: This takes an array of attribute names (as strings) for attributes to exclude from the JSON.
  • :only: This takes an array of attribute names (as strings) for the only attributes to include in the JSON.

For example:

Get hands-on with 1200+ tech skills courses.