Skip to main content

need How to set content-type header as JSON for result json in controller Magento 2.3

I am having the Controllercontroller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as Json JSON. I want the JsonJSON result in the tree(PRETTY) format. Kindly help me.

\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
.
.
$resultJson = $this->_resultJsonFactory->create();
.
.
$resultJson->setHeader('Content-type', 'application/json',true);
$resultJson->setData(["view_url"=> "/","results"=>$results]);
       
return $resultJson;


which is resulting with:
{"view_url":"\/","results":[{"product_image":"\/p11.jpg","product_name":"name1","product_desc":"protection.","absorbency":"hik","special_size":"Yes","product_url":"\/"},{"product_image":"\/p1.jpg","product_name":"name2","product_desc":" protection.","absorbency":"high","special_size":"No","product_url":"\/"}]}

But i need like this: (with response header as JSON)
{
  "view_url": "/",
  "results": [
    {
      "product_image": "/p11.jpg",
      "product_name": "name1",
      "product_desc": "protection.",
      "absorbency": "hik",
      "special_size": "Yes",
      "product_url": "/"
    },
    {
      "product_image": "/p1.jpg",
      "product_name": "name2",
      "product_desc": " protection.",
      "absorbency": "high",
      "special_size": "No",
      "product_url": "/"
    }
  ]
}\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
.
.
$resultJson = $this->_resultJsonFactory->create();
.
.
$resultJson->setHeader('Content-type', 'application/json',true);
$resultJson->setData(["view_url"=> "/","results"=>$results]);
       
return $resultJson;

which is resulting with:

{"view_url":"\/","results":[{"product_image":"\/p11.jpg","product_name":"name1","product_desc":"protection.","absorbency":"hik","special_size":"Yes","product_url":"\/"},{"product_image":"\/p1.jpg","product_name":"name2","product_desc":" protection.","absorbency":"high","special_size":"No","product_url":"\/"}]}

But i need like this: (with response header as JSON)

{
  "view_url": "/",
  "results": [
    {
      "product_image": "/p11.jpg",
      "product_name": "name1",
      "product_desc": "protection.",
      "absorbency": "hik",
      "special_size": "Yes",
      "product_url": "/"
    },
    {
      "product_image": "/p1.jpg",
      "product_name": "name2",
      "product_desc": " protection.",
      "absorbency": "high",
      "special_size": "No",
      "product_url": "/"
    }
  ]
}

need header as JSON for result json in controller Magento 2.3

I am having the Controller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as Json . I want the Json result in the tree(PRETTY) format. Kindly help me

\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
.
.
$resultJson = $this->_resultJsonFactory->create();
.
.
$resultJson->setHeader('Content-type', 'application/json',true);
$resultJson->setData(["view_url"=> "/","results"=>$results]);
       
return $resultJson;


which is resulting with:
{"view_url":"\/","results":[{"product_image":"\/p11.jpg","product_name":"name1","product_desc":"protection.","absorbency":"hik","special_size":"Yes","product_url":"\/"},{"product_image":"\/p1.jpg","product_name":"name2","product_desc":" protection.","absorbency":"high","special_size":"No","product_url":"\/"}]}

But i need like this: (with response header as JSON)
{
  "view_url": "/",
  "results": [
    {
      "product_image": "/p11.jpg",
      "product_name": "name1",
      "product_desc": "protection.",
      "absorbency": "hik",
      "special_size": "Yes",
      "product_url": "/"
    },
    {
      "product_image": "/p1.jpg",
      "product_name": "name2",
      "product_desc": " protection.",
      "absorbency": "high",
      "special_size": "No",
      "product_url": "/"
    }
  ]
}

How to set content-type header as JSON in controller Magento 2.3

I am having the controller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as JSON. I want the JSON result in the tree(PRETTY) format. Kindly help me.

\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
.
.
$resultJson = $this->_resultJsonFactory->create();
.
.
$resultJson->setHeader('Content-type', 'application/json',true);
$resultJson->setData(["view_url"=> "/","results"=>$results]);
       
return $resultJson;

which is resulting with:

{"view_url":"\/","results":[{"product_image":"\/p11.jpg","product_name":"name1","product_desc":"protection.","absorbency":"hik","special_size":"Yes","product_url":"\/"},{"product_image":"\/p1.jpg","product_name":"name2","product_desc":" protection.","absorbency":"high","special_size":"No","product_url":"\/"}]}

But i need like this: (with response header as JSON)

{
  "view_url": "/",
  "results": [
    {
      "product_image": "/p11.jpg",
      "product_name": "name1",
      "product_desc": "protection.",
      "absorbency": "hik",
      "special_size": "Yes",
      "product_url": "/"
    },
    {
      "product_image": "/p1.jpg",
      "product_name": "name2",
      "product_desc": " protection.",
      "absorbency": "high",
      "special_size": "No",
      "product_url": "/"
    }
  ]
}
added 1180 characters in body
Source Link

I am having the Controller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as Json . I want the Json result in the tree(PRETTY) format. Kindly help me

This is my code:

\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
.
.
$resultJson = $this->_resultJsonFactory->create();
.
.
$resultJson->setHeader('Content-type', 'application/json',true);
$resultJson->setData(["view_url"=> "/","results"=>$results]);
       
return $resultJson;


which is resulting with:
{"view_url":"\/","results":[{"product_image":"\/p11.jpg","product_name":"name1","product_desc":"protection.","absorbency":"hik","special_size":"Yes","product_url":"\/"},{"product_image":"\/p1.jpg","product_name":"name2","product_desc":" protection.","absorbency":"high","special_size":"No","product_url":"\/"}]}

But i need like this: (with response header as JSON)
{
  "view_url": "/",
  "results": [
    {
      "product_image": "/p11.jpg",
      "product_name": "name1",
      "product_desc": "protection.",
      "absorbency": "hik",
      "special_size": "Yes",
      "product_url": "/"
    },
    {
      "product_image": "/p1.jpg",
      "product_name": "name2",
      "product_desc": " protection.",
      "absorbency": "high",
      "special_size": "No",
      "product_url": "/"
    }
  ]
}

I am having the Controller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as Json . I want the Json result in the tree(PRETTY) format. Kindly help me

I am having the Controller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as Json . I want the Json result in the tree(PRETTY) format. Kindly help me

This is my code:

\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
.
.
$resultJson = $this->_resultJsonFactory->create();
.
.
$resultJson->setHeader('Content-type', 'application/json',true);
$resultJson->setData(["view_url"=> "/","results"=>$results]);
       
return $resultJson;


which is resulting with:
{"view_url":"\/","results":[{"product_image":"\/p11.jpg","product_name":"name1","product_desc":"protection.","absorbency":"hik","special_size":"Yes","product_url":"\/"},{"product_image":"\/p1.jpg","product_name":"name2","product_desc":" protection.","absorbency":"high","special_size":"No","product_url":"\/"}]}

But i need like this: (with response header as JSON)
{
  "view_url": "/",
  "results": [
    {
      "product_image": "/p11.jpg",
      "product_name": "name1",
      "product_desc": "protection.",
      "absorbency": "hik",
      "special_size": "Yes",
      "product_url": "/"
    },
    {
      "product_image": "/p1.jpg",
      "product_name": "name2",
      "product_desc": " protection.",
      "absorbency": "high",
      "special_size": "No",
      "product_url": "/"
    }
  ]
}
Source Link

need header as JSON for result json in controller Magento 2.3

I am having the Controller which is responding with resultJson(\Magento\Framework\Controller\Result\JsonFactory), I have given setData. And I need to set header as Json . I want the Json result in the tree(PRETTY) format. Kindly help me