1

i need to return the response of custom web api as JSON.

i need it to be like this:

{
  "data": [
    {
      "id": "1",
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "id": "2",
      "name": "Garrett Winters",
      "position": "Accountant",
      "salary": "$170,750",
      "start_date": "2011/07/25",
      "office": "Tokyo",
      "extn": "8422"
    }
  ]
}

however, my module is coming out as

[
    {
      "id": "1",
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "id": "2",
      "name": "Garrett Winters",
      "position": "Accountant",
      "salary": "$170,750",
      "start_date": "2011/07/25",
      "office": "Tokyo",
      "extn": "8422"
    }
]

My Interface is as follows:

<?php
namespace Mag\Rest\Api;

interface OrdersManagementInterface
{
  /**
   * GET for Stock api
   * @param mixed $items
   * @return string
   */
  public function getOrders();

}

My model is as follows:

<?php
namespace Mag\Rest\Model;

class OrdersManagement
{
    public function __construct(
    ) {
    }
    /**
     * {@inheritdoc}
     */
    public function getOrders()
    {
        //... sql code to get data goes here
        $results = $this->connection->fetchAll($sql);
        return  $response ;
    }
}

any idea on how to be able to format the output of the rest api ?

Zorox
  • 2,040
  • 2
  • 21
  • 28

0 Answers0