title: How can I create a json string in the right format with serializer (Symfony 4)?
body: <p>I am creating a json string with serializer:</p>
  
  <pre><code>$table = $this-&gt;getDoctrine()-&gt;getRepository(Article::class)-&gt;findAll();
  
  $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' =&gt; new JsonEncoder()));
  $json_string = $serializer-&gt;serialize($table, 'json');
  </code></pre>
  
  <p>the result of $table:</p>
  
  <pre><code>array(2) { [0]=&gt; object(App\Entity\Article)#5975 (3) { ["id":"App\Entity\Article":private]=&gt; int(1) ["title":"App\Entity\Article":private]=&gt; string(9) "Article 1" ["body":"App\Entity\Article":private]=&gt; string(32) "This is the body for article one" } [1]=&gt; object(App\Entity\Article)#5979 (3) { ["id":"App\Entity\Article":private]=&gt; int(2) ["title":"App\Entity\Article":private]=&gt; string(11) "Article Two" ["body":"App\Entity\Article":private]=&gt; string(32) "This is the body for article two" } } 
  </code></pre>
  
  <p>This is the result of $json_string:</p>
  
  <pre><code>string(145) "[{"id":1,"title":"Article 1","body":"This is the body for article one"},{"id":2,"title":"Article Two","body":"This is the body for article two"}]" 
  </code></pre>
  
  <p>But it is not the correct format that I need. I need it to be encoded like this:</p>
  
  <pre><code>{
    "recordsTotal": 10,
    "recordsFiltered": 10,
    "draw": 1,
    "data": [
      [
        "Airi",
        "Satou",
        "Accountant",
        "Tokyo",
        "28th Nov 08",
        "$162,700"
      ],
      [
        "Angelica",
        "Ramos",
        "Chief Executive Officer (CEO)",
        "London",
        "9th Oct 09",
        "$1,200,000"
      ],
      [
        "Ashton",
        "Cox",
        "Junior Technical Author",
        "San Francisco",
        "12th Jan 09",
        "$86,000"
      ],
      [
        "Bradley",
        "Greer",
        "Software Engineer",
        "London",
        "13th Oct 12",
        "$132,000"
      ],
      [
        "Brenden",
        "Wagner",
        "Software Engineer",
        "San Francisco",
        "7th Jun 11",
        "$206,850"
      ],
      [
        "Brielle",
        "Williamson",
        "Integration Specialist",
        "New York",
        "2nd Dec 12",
        "$372,000"
      ],
      [
        "Bruno",
        "Nash",
        "Software Engineer",
        "London",
        "3rd May 11",
        "$163,500"
      ],
      [
        "Caesar",
        "Vance",
        "Pre-Sales Support",
        "New York",
        "12th Dec 11",
        "$106,450"
      ],
      [
        "Cara",
        "Stevens",
        "Sales Assistant",
        "New York",
        "6th Dec 11",
        "$145,600"
      ],
      [
        "Cedric",
        "Kelly",
        "Senior Javascript Developer",
        "Edinburgh",
        "29th Mar 12",
        "$433,060"
      ]
    ]
  }
  </code></pre>
  
  <p>Aproach with Normalizer:</p>
  
  <pre><code>$normalizer = new ObjectNormalizer();
  $encoder = new JsonEncoder();
  $serializer = new Serializer(array($normalizer), array($encoder));
  $json_string = $serializer-&gt;serialize($table, 'json');
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>string(145) "[{"id":1,"title":"Article 1","body":"This is the body for article one"},{"id":2,"title":"Article Two","body":"This is the body for article two"}]" 
  </code></pre>