This is my settings in view.

enter image description here

Data what I have is now in this format:


I would like to change this format in this, rid of all comments. :


How I can achieve this?

1 Answer 1


To change the structure of the Serializer output you should create a custom Serializer here is the steps of your use case:

  1. Create CustomSerializer extends the Drupal\rest\Plugin\views\style\Serializer and update the render method to your needs.
    • Create MY_MODULE/Plugin/views/style/CustomSerializer.php

namespace Drupal\MY_MODULE\Plugin\views\style;

use Drupal\rest\Plugin\views\style\Serializer;

 * The style plugin for serialized output formats.
 * @ingroup views_style_plugins
 * @ViewsStyle(
 *   id = "custom_serializer",
 *   title = @Translation("Custom serializer"),
 *   help = @Translation("Serializes views row data using the Serializer
 *   component."), display_types = {"data"}
 * )
class CustomSerializer extends Serializer {

   * {@inheritdoc}
  public function render() {
    $rows = [];
    // If the Data Entity row plugin is used, this will be an array of entities
    // which will pass through Serializer to one of the registered Normalizers,
    // which will transform it to arrays/scalars. If the Data field row plugin
    // is used, $rows will not contain objects and will pass directly to the
    // Encoder.
    foreach ($this->view->result as $row_index => $row) {
      $this->view->row_index = $row_index;
      $row_render = $this->view->rowPlugin->render($row);
      $iot_integer = 0;
      if (isset($row_render["field_iot_integer"]) && $row_render["field_iot_integer"] instanceof \Drupal\Core\Render\Markup){
        $iot_integer = (int) $row_render["field_iot_integer"]->__toString();
     $rows[] = [

    // Get the content type configured in the display or fallback to the
    // default.
    if ((empty($this->view->live_preview))) {
      $content_type = $this->displayHandler->getContentType();
    else {
      $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
    return $this->serializer->serialize($rows, $content_type, ['views_style_plugin' => $this]);

  1. Clear Cache:
  2. Go to your view and change the format from Serializer to Custom serializer like enter image description here

  3. The result:

enter image description here

  • Thank you a lot. This is very good. I understood that I was need to extend class Serializer but didn't know to write function.
    – SakaSerbia
    Commented Mar 7, 2019 at 13:01
  • 1
    you are welcome, glad that help.
    – berramou
    Commented Mar 7, 2019 at 13:56
  • Hi i have the same problem, but when i've created those files i dont have that option "Custom serializer" in views, could you please help me? Commented Mar 3, 2021 at 20:24
  • Did you put the files in the right structure, and you check that your module is enabled and did you cleared the cache ?
    – berramou
    Commented Mar 4, 2021 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.