0

How I can change the json format from

[
    [
        {
            "success": [
                {
                    "code": 200,
                    "response": {
                        "collection": [
                            {
                                "id": "1",
                                "title": "camp1",
                                "description": "vishal desc",
                                "start_date": "2019-02-28 00:00:00",
                                "end_date": "2001-02-22 00:00:00",
                                "status": "0",
                                "desktop_image": "images\\yourope.jpg",
                                "mobile_image": "images\\yourope.jpg",
                                "created_at": "2019-02-28 06:50:25",
                                "updated_at": "2019-03-11 10:16:48",
                                "other": null
                            }
                        ]
                    }
                }
            ]
        }
    ]
]

To this

{
   "data": [
        {
            "success": [
                {
                    "code": 200,
                    "response": {
                        "collection": [
                            {
                                "id": "1",
                                "title": "camp1",
                                "description": "vishal desc",
                                "start_date": "2019-02-28 00:00:00",
                                "end_date": "2001-02-22 00:00:00",
                                "status": "0",
                                "desktop_image": "images\\yourope.jpg",
                                "mobile_image": "images\\yourope.jpg",
                                "created_at": "2019-02-28 06:50:25",
                                "updated_at": "2019-03-11 10:16:48",
                                "other": null
                            }
                        ]
                    }
                }
            ]
        }
    ]
}
  • assign your result to a multidimensional array with key 'data' – Purnendar Gattu Mar 26 at 12:25
  • Thanks for reply. I try out this but is not working if i am not wrong may be i need to put this "data" while final JSON has been return by system as we doing in D:\wamp64\www\artoreal\app\code\core\Mage\Api2\Model\Renderer\json.php file public function render($data) { /* Added By Nihilent For Customize JSON response*/ //return Zend_Json::encode($data); if($data['messages']){ return json_encode(array('data'=>array_values($data))); }else{ return Zend_Json::encode($data); } } – Vishal Bansal Mar 28 at 9:39
  • Hi vishal, can you post the method how you rendering , so i can check it – Purnendar Gattu Mar 29 at 5:44
  • public function getinfo($id) { $post = $this->_advertisementFactory->create(); $collection = $post->getCollection(); foreach($collection as $item){ if($item['id']==$id){ $data[]=$item->getData(); } } $response['data'][]=array('success'=>array(array('code'=>200,'response'=>array('collection'=>$data)))); return $response; } } – Vishal Bansal Mar 29 at 6:00
0

Finally I got the answer:

  1. Overwrite this file:

    \Magento\Framework\Webapi\Rest\Response\Renderer\Json.php

  2. We are overwrite this file : di.xml

    preference for="Magento\Framework\Webapi\Rest\Response\Renderer\Json" type="Evolve\Apirenderer\Webapi\Rest\Response\Renderer\Json"

  3. Replace this function by this Orignal:

    public function render($data)
    {
        return $this->encoder->encode($data);
    }   
    

    Replace By This

    public function render($data)
    {
       return json_encode(array('data'=>array_values($data)));
    }
    

    You will get above response.

  • please format the code, hard to understand – jafar pinjar May 23 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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