How_to_send_the_JSON_data_from_a_Drupal_8_1

How to send the JSON data from a Drupal 8 site?

How to get Drupal 8 site data to a mobile app

Imagine a situation: your mobile application needs to get some information from your site on Drupal 8 using JSON. How to do it without much effort and installing additional modules, how to change the JSON array programmatically, and send the JSON data with and without using Views - all of these you will learn from this article.

→ This post is about an older version of Drupal. It can still be useful to you but you can also check out our latest article about Drupal 10 here: What Do You Need to Know About Drupal 10?

Several ways to create and send data in the JSON format in Drupal 8

Good news! You do not need to write code or download additional modules to configure the sending of JSON data using REST and to customize a JSON array. Everything you need is already in the core of Drupal 8. First of all, you need to install the Views and Rest modules and determine an exported content type. If you have never worked and are not familiar with the Rest module, I recommend that you familiarize yourself with this guide before reading the rest of the article.

Send the JSON data with REST and Views

Step 1

Create a new view. At this step, you just select a name, and content type, click “REST EXPORT SETTINGS”, and set a path via which you will later get the data in the JSON format.

Create a new view

Step 2

Choose fields. If you choose an entity, JSON will send all the node fields at once but if you select fields, you can specify the fields that you want to send.

Choosing the style of content in view

Step 3

Choose how this view should be styled, I choose a standard option - Serializer (Region Format, Show -> settings).

Choose how this view should be styled

Step 4

Choose the JSON format. (Region Format, Serializer -> settings).

Style options - JSON

In the view, I selected ID, title, and body fields. So, the view will form an array only from the data that is contained in these fields.

How the view should look like in the end

With the help of the extension for Chrome “Rest API Testing” I could get this result at the address of the created view (http://hostname/export).
JSON result in the Chrome plugin

Send JSON data programmatically with Normalizer

With Views, you can customize a JSON array and specify only the certain fields that will be returned. But what if your application needs to receive the JSON array in a specific form that you can not configure in Views? You can take advantage of Normalizer. In the custom module, we create the file “services” and necessarily set the priority higher or equal to 2, so that a custom normalizer is executed with the highest priority. Otherwise, nothing will work.

Next, add a class to the / src / Normalizer folder. Since the Rest module allows different content on a site to be received in the JSON format, with the help of a custom normalizer, we can redefine a type of the given array for the event content type (or for any other type).

Thus, we can also redefine the output of our view that was created in the previous method.

Send JSON data with Controller

However, redefining an array through normalizers can be too inconvenient if you need to get the absolutely custom JSON array from a specific path. In addition, an override of the normalizer will affect the JSON structure of all the nodes of this content type, that’s why you can use a simpler method: you can always send the JSON data using the controller having adjusted this data to a certain path. And this method can really help to solve a more specific problem, for example, in my case, it was necessary to place all the nodes in the common array, which is very difficult or even impossible to achieve through a view. You can learn how to create a custom module with a controller in this article.

First, create a routing file and assign a path which will use the GET method to get JSON.

The controller file will look like in the code sample below. Using the custom method in the controller and JSONResponse, we can send the JSON array exactly as we want it to. Here we do not use hardcoded Symfony and the Rest module methods in order to change the JSON array but create our own.

Conclusion

I showed you several ways how you can work with REST and JSON in Drupal 8, each of them was useful to me to some extent and I hope that they will reduce the time to find a solution to a particular problem.

If you want to use any of those solutions, you may find the code at GitHub.

We are always ready to help you to connect a Drupal 8 website with a mobile app.

Useful links: 

1. OOP in Drupal 8 and how to use it to create a custom module

2. RESTful Web Services in Drupal 8 quick start guide

3. How to create a headless Drupal site

4. Why you should migrate your site to Drupal

You might also like

Claro Admin Theme

After 11 years, the Drupal admin panel is getting a new default theme. We have explored its pros and bet it has never been so easy and enjoyable to manage Drupal sites before.

What you need to know about cyberattacks in 2023

Practice shows that cyber criminals have no boundaries and their guile leads to hundreds of millions in damage to businesses. What type of cyberattacks are most common and how can you protect your website from hacking and leaks? Read our post to learn.