Introduction of Drupal JSON:API

20 / Oct / 2023 by Akash Kumar 0 comments

JSON API is a standardized specification for building APIs in JSON format. It aims to simplify and streamline the process of creating and consuming APIs by providing a clear and consistent structure for data exchange. JSON API is a core module to access Drupal’s data structures, i.e., entity types, bundles, fields, etc. It supports the ability to build decoupled applications or integrate Drupal with other systems.

Enabling JSON API

Visit: /admin/config/services/jsonapi and check the “Accept all JSON:API create, read, update, and delete operations.” option to allow POST, PATCH, and DELETE operations.

Understanding JSON API

Every resource in JSON:API must have a globally unique type of property. The Drupal JSON:API implementation derives this type property from the entity type machine name and bundle machine name. For example, articles, pages, and users are given the types of nodearticle, nodepages, and useruser, respectively.

Example of JSON:API URL structure

GET | POST:           /jsonapi/node/article

PATCH | DELETE:  /jsonapi/node/article/{uuid}

JSON: API accepts the following HTTP methods

  • GET – Retrieve data, which can be a collection of resources or an individual resource
  • POST – Create a new resource
  • PATCH – Update an existing resource
  • DELETE – Remove an existing resource

GET requests:

The following headers are required on all Get requests.

  • Accept: application/vnd.api+json
  • Authorization:Basic VOxwmuUua_0


To get JSON:API object of a single article node, including attributes, relationships, and links.
URL: http://example.com/jsonapi/node/article/{{article_uuid}}

Note: JSON Api works with uuid, not nid. You can find uuid of an entity using the Devel module.

Response

HTTP 200 response. The response body contains the JSON:API object of a single article node, including attributes, relationships, and links.

Post requests

The following headers are required on all POST request to get a proper JSON:API request and response.

  • Accept: application/vnd.api+json
  • Content-Type:application/vnd.api+json
  • Authorization:Basic VOxwmuUua_0

Note: You can get access token by /session/token.

Basic POST request

URL: http://example.com/jsonapi/node/article

Response

HTTP 201 (Created) response. The response body contains the JsonApi response of the created entity.

Delete Request:

The following headers are used for All DELETE request :

  • Content-Type:application/vnd.api+json
  • Authorization:Basic VOxwmuUua_0

Basic DELETE request

URL: http://example.com/jsonapi/node/article/{{article_uuid}}

Response

HTTP 204 (No content) response. Empty response body.

Patch Request

The following header is needed for all patch requests:

  • Accept: application/vnd.api+json
  • Content-Type:application/vnd.api+json
  • Authorization:Basic VALUE

Basic PATCH request

URL: http://example.com/jsonapi/node/article/{{article_uuid}}

Response

HTTP 200 response. The response body with the JsonApi response of the updated entity.

Features of JSON API

Automatic Serialization: JSON API handles the serialization of data for you. When you request data from Drupal, it’s automatically transformed into a JSON API-compliant format, reducing the need for custom serialization code.

Pagination and Sorting: JSON API supports pagination and sorting out of the box. This is particularly useful for large datasets, ensuring that clients can retrieve data efficiently.

Relationships: JSON API allows you to retrieve related resources in a single request, reducing the number of API calls needed to fetch interconnected data.

Filtering and Querying: You can filter and query Drupal resources using query parameters in the URL, allowing you to request only the data you need.

Benefits of Using JSON API in Drupal 10

1. Simplified Development: JSON API simplifies the development of web applications by providing a standardized way to interact with Drupal’s data

2. Efficiency: With automatic serialization and support for relationships, JSON API reduces the complexity of handling data, leading to more efficient development.

3. Compatibility: JSON API is widely supported across various programming languages and frameworks, making it easy to integrate Drupal with a wide range of technologies.

4. Scalability: JSON API’s RESTful design allows your Drupal site to scale effortlessly, handling a growing number of clients and requests.

Conclusion

JSON API is a powerful tool that exposes content in a flexible way and in a standardized format. It simplifies the process of building a decoupled application or integrating Drupal with other services and promotes best practices in API design.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *