Imagine a situation: your mobile application needs to get some information from your site on Drupal 8 using JSON. Why JSON? Why not XML? My client needed the data in the JSON format, but still, why choose JSON in our case? On the client application side, JSON data processing is very simple. On the other hand, the data sent in JSON format can have security problems. So it’s better to use XML on the server and JSON for the client-side.
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.
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 a 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
Create a new view. At this step, you just select a name, content type, click “REST EXPORT SETTINGS”, and set a path via which you will later get the data in the JSON format.
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.
Choose how this view should be styled, I choose a standard option — Serializer (Region Format, Show -> settings).
Choose the JSON format. (Region Format, Serializer -> settings).
In the view, I selected ID, title, and body fields. So, the view will form an array only from the data that are contained in these fields.
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).
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.
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.
Originally posted at the ADCI Solutions website.