Add computed field to JSON:API response

Drupal 8 and 9 provide an out-of-the-box JSON:API implementation. This is because JSON:API module has been added to the core in version 8.7. You can see the change record here: #3041438

Let's see how to add a field to the response.

There is more that one way to do this, but this time I will show you how to use computed fields to do it. These fields are not stored in the database like regular fields, but they are cached if you have the cache module enabled.

To add a computed field you first have to implement hook entity base field info alter in your .module file like this:

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\MODULE_NAME\Plugin\Field\MyComputedField;

 * Implements hook_entity_base_field_info_alter().
function MODULE_NAME_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'node') {
    $fields['my_computed_field'] = BaseFieldDefinition::create('string')
      ->setLabel(t('My computed field'))
      ->setName('My computed field')
      ->setDescription(t('My computed field description.'))

This adds a computed field called my_computed_field to all nodes. Now, let's add MyComputedField class.


namespace Drupal\MODULE_NAME\Plugin\Field;

use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;

class MyComputedField extends FieldItemList {

  use ComputedItemListTrait;

   * Computes the field value.
  protected function computeValue() {
    $this->list[0] = $this->createItem(0, 'My computed field value');


This class file is located under src/Plugin/Field directory.

If you clear the cache, you will see your new field in the JSON:API output for node resources.

This also works for the RESTful Web Services module that is in Drupal core!

One thing to note is that you cannot add filters on computed fields. This is because filtering in JSON:API module is built directly on top of the core entity query API. And because core doesn't support filtering computed fields, JSON:API is also not supporting that operation.

This might change in the future as you can see in this issue: #2962457 but for now, that can't be done.

About the Author

Goran Nikolovski is a creator, speaker, open-source contributor, web developer specialized in Drupal and DevOps enthusiast. He is the founder of this website and he enjoys sharing his knowledge and experiences.