Overview

This is a personal note on how to use Drupal’s JSON:API. This time, I will cover the use of include for taxonomies and multilingual processing.

Data

As shown below, the taxonomy “Assistant Professor” has been assigned to the position field.

/node/5

Additionally, content multilingualization is enabled, so the English versions of the title and position are also displayed as shown below.

/en/node/5

JSON:API

Since the above content was created with the content type “faculty”, the data list can be retrieved from the following URL.

/jsonapi/node/faculty/

Below shows the result excluding the links field. While the taxonomy ID is included in field_position, the label of that taxonomy is not included.

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [
    {
      "type": "node--faculty",
      "id": "586ef1d9-b680-41f9-b54b-7ebcdc9d154f",
      "attributes": {
        "drupal_internal__nid": 5,
        "drupal_internal__vid": 13,
        "langcode": "ja",
        "revision_timestamp": "2023-06-08T01:01:43+00:00",
        "revision_log": null,
        "status": true,
        "title": "中村覚",
        "created": "2023-06-08T00:44:15+00:00",
        "changed": "2023-06-08T01:01:26+00:00",
        "promote": true,
        "sticky": false,
        "default_langcode": true,
        "revision_translation_affected": null,
        "content_translation_source": "und",
        "content_translation_outdated": false,
        "path": {
          "alias": null,
          "pid": null,
          "langcode": "ja"
        },
        "body": null
      },
      "relationships": {
        "node_type": {
          "data": {
            "type": "node_type--node_type",
            "id": "841962f7-91c8-47a1-b335-ea494efe467c",
            "meta": {
              "drupal_internal__target_id": "faculty"
            }
          }
        },
        "revision_uid": {
          "data": {
            "type": "user--user",
            "id": "0b001e4d-ed29-4a53-960d-9cdcc3d3ad70",
            "meta": {
              "drupal_internal__target_id": 1
            }
          }
        },
        "uid": {
          "data": {
            "type": "user--user",
            "id": "0b001e4d-ed29-4a53-960d-9cdcc3d3ad70",
            "meta": {
              "drupal_internal__target_id": 1
            }
          }
        },
        "field_position": {
          "data": {
            "type": "taxonomy_term--position",
            "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
            "meta": {
              "drupal_internal__target_id": 5
            }
          }
        }
      }
    }
  ]
}

Using include

Add ?include=field_position to the query. As a result, the included field is added, and the value of the taxonomy term’s name field can also be obtained.

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [
    {
      "type": "node--faculty",
      "id": "586ef1d9-b680-41f9-b54b-7ebcdc9d154f",
      "attributes": {
        "drupal_internal__nid": 5,
        "drupal_internal__vid": 13,
        "langcode": "ja",
        "revision_timestamp": "2023-06-08T01:01:43+00:00",
        "revision_log": null,
        "status": true,
        "title": "中村覚",
        "created": "2023-06-08T00:44:15+00:00",
        "changed": "2023-06-08T01:01:26+00:00",
        "promote": true,
        "sticky": false,
        "default_langcode": true,
        "revision_translation_affected": null,
        "content_translation_source": "und",
        "content_translation_outdated": false,
        "path": {
          "alias": null,
          "pid": null,
          "langcode": "ja"
        },
        "body": null
      },
      "relationships": {
        "node_type": {
          "data": {
            "type": "node_type--node_type",
            "id": "841962f7-91c8-47a1-b335-ea494efe467c",
            "meta": {
              "drupal_internal__target_id": "faculty"
            }
          }
        },
        "revision_uid": {
          "data": {
            "type": "user--user",
            "id": "0b001e4d-ed29-4a53-960d-9cdcc3d3ad70",
            "meta": {
              "drupal_internal__target_id": 1
            }
          }
        },
        "uid": {
          "data": {
            "type": "user--user",
            "id": "0b001e4d-ed29-4a53-960d-9cdcc3d3ad70",
            "meta": {
              "drupal_internal__target_id": 1
            }
          }
        },
        "field_position": {
          "data": {
            "type": "taxonomy_term--position",
            "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
            "meta": {
              "drupal_internal__target_id": 5
            }
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "taxonomy_term--position",
      "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
      "attributes": {
        "drupal_internal__tid": 5,
        "drupal_internal__revision_id": 5,
        "langcode": "ja",
        "revision_created": "2023-06-08T01:00:31+00:00",
        "revision_log_message": null,
        "status": true,
        "name": "助教",
        "description": null,
        "weight": 0,
        "changed": "2023-06-08T01:00:31+00:00",
        "default_langcode": true,
        "revision_translation_affected": true,
        "content_translation_source": "und",
        "content_translation_outdated": false,
        "content_translation_created": "2023-06-08T01:00:31+00:00",
        "path": {
          "alias": null,
          "pid": null,
          "langcode": "ja"
        }
      },
      "relationships": {
        "vid": {
          "data": {
            "type": "taxonomy_vocabulary--taxonomy_vocabulary",
            "id": "450e8e85-4866-4373-97c2-db259ac381ff",
            "meta": {
              "drupal_internal__target_id": "position"
            }
          }
        },
        "revision_user": {
          "data": null
        },
        "parent": {
          "data": [
            {
              "type": "taxonomy_term--position",
              "id": "virtual"
            }
          ]
        },
        "content_translation_uid": {
          "data": {
            "type": "user--user",
            "id": "0b001e4d-ed29-4a53-960d-9cdcc3d3ad70",
            "meta": {
              "drupal_internal__target_id": 1
            }
          }
        }
      }
    }
  ]
}

Furthermore, by using the fields query, you can reduce the size of the retrieved results as shown below.

/jsonapi/node/faculty/?include=field_position&fields[node–faculty]=title,field_position&fields[taxonomy_term–position]=name

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [
    {
      "type": "node--faculty",
      "id": "586ef1d9-b680-41f9-b54b-7ebcdc9d154f",
      "attributes": {
        "title": "中村覚"
      },
      "relationships": {
        "field_position": {
          "data": {
            "type": "taxonomy_term--position",
            "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
            "meta": {
              "drupal_internal__target_id": 5
            }
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "taxonomy_term--position",
      "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
      "attributes": {
        "name": "助教"
      }
    }
  ]
}

Multilingual Support

Simply by appending the language to the URL, data in that language can be retrieved.

/en/jsonapi/node/faculty/?include=field_position&fields[node–faculty]=title,field_position&fields[taxonomy_term–position]=name

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [
    {
      "type": "node--faculty",
      "id": "586ef1d9-b680-41f9-b54b-7ebcdc9d154f",
      "attributes": {
        "title": "Satoru Nakamura"
      },
      "relationships": {
        "field_position": {
          "data": {
            "type": "taxonomy_term--position",
            "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
            "meta": {
              "drupal_internal__target_id": 5
            }
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "taxonomy_term--position",
      "id": "6ce458e8-6d79-4ed1-9653-5ec178568b7a",
      "attributes": {
        "name": "assistant professor"
      }
    }
  ]
}

Summary

Many features are implemented, and I found it very convenient. I hope this serves as a useful reference for others.