0

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": "/"
 }
 ]
}
asked Mar 4, 2020 at 5:55
1
  • Could you please more clarify what is your current result and what is your expected. Commented Mar 4, 2020 at 5:56

1 Answer 1

0

After looking at your JSON response I don't see any issue. Validated here.

I don't think you will see any issue in decoding this response if you wanna read it further because format is valid.

If you wanna see it beautiful in your browser then you can use browser extension. I use Chrome Extension JSONView. Check here.

Hope this is helpful. Cheers!

answered Mar 5, 2020 at 9:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.