3

This post is related to this Magento2 - How to get JSON response from Controller

What I am trying to do is to format the json response of my custom web API, but it seems \Magento\Framework\Controller\Result\JsonFactory doesn't seem to work in Magento 2.3

Here's my implementation:

/** @var \Magento\Framework\Controller\Result\JsonFactory  */
protected $resultJsonFactory;

public function __construct(
    ...
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
    ...
) {
    ...
    $this->resultJsonFactory = $resultJsonFactory;
    ...
}  

And implemented it like this:

$result = $this->resultJsonFactory->create();
$result->setData(['message' => 'test']);
return $result;  

And the result (Just a blank bracket):

[]

Additional solution made:
Tried to implement \Zend_Json::encode() and json_encode but getting this result:

"{\"message\":\"test\"}"

Instead of (expected result/prettified json response):

{
    "message": "test"
}

5 Answers 5

0

Try this,

$result = $this->resultJsonFactory->create();
$myArray = [];
$myArray['message'] = 'test';
$result->setData($myArray);
return $result;  

Hope this helps.

8
  • same I got blank brackets
    – fmsthird
    Commented Jul 19, 2019 at 10:15
  • can you try after cache:flush?
    – Mohit Rane
    Commented Jul 19, 2019 at 10:16
  • sure. I will ..let me check now
    – fmsthird
    Commented Jul 19, 2019 at 10:16
  • just did it now and getting the same result
    – fmsthird
    Commented Jul 19, 2019 at 10:17
  • try this $myArray=array("message" => "test"); instead of $myArray = []; $myArray['message'] = 'test';
    – Mohit Rane
    Commented Jul 19, 2019 at 10:18
0

In your controller,

protected $jsonFactory;

public function __construct(
      \Magento\Backend\App\Action\Context $context,
      \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
) {
      parent::__construct($context);
      $this->jsonFactory = $jsonFactory;
 }

 public function execute()
 {
     $resultJson = $this->jsonFactory->create();
     $messages = [];
     $messages[] = __('Your message.');
     return $resultJson->setData([
           'messages' => $messages
     ]);
 }
5
  • just tried it really quick and got the same result
    – fmsthird
    Commented Jul 19, 2019 at 11:08
  • what is the result you are getting?
    – aravind
    Commented Jul 19, 2019 at 11:24
  • still, blank brackets
    – fmsthird
    Commented Jul 19, 2019 at 11:48
  • How you are trying to get that message?
    – aravind
    Commented Jul 19, 2019 at 11:49
  • I'm using postman
    – fmsthird
    Commented Jul 19, 2019 at 11:50
0

In my extension I post to ajax controller and response is in json

https://github.com/DominicWatts/BackInStock/blob/master/Controller/Index/Submit.php#L107-L110

https://github.com/DominicWatts/BackInStock/blob/master/view/frontend/templates/product/view/addtocart.phtml#L95

Works for me. Bit different to what you've tried so far. I hope it helps. Good luck.

Update

Have you tried accessing getResponse from context

https://github.com/DominicWatts/CustomerApprove/blob/master/Plugin/Magento/Customer/Controller/Account/LoginPost.php

    public function __construct(
        Context $context,
        CustomerRepositoryInterface $customerRepositoryInterface,
        ManagerInterface $messageManager
    ) {
        $this->_request = $context->getRequest();
        $this->_response = $context->getResponse();
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->resultFactory = $context->getResultFactory();
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->messageManager = $messageManager;
    }
1
  • thanks for the answer but I got an error about getResponse() method maybe because I am implementing it in a model, not in a controller. I'm not sure
    – fmsthird
    Commented Jul 22, 2019 at 3:30
0

The reason why you get blank brackets is because webapi has its own renderer which is trying to convert whole Magento\Framework\Controller\Result\Json object into json.

You should return just the data instead and renderer will convert it.

Use this

return ['message' => 'test'];

Instead of this

$result = $this->resultJsonFactory->create();
$result->setData(['message' => 'test']);
return $result;
0

I have the same issue, i found that JsonFactory doesnt work for me in webapi, so there is another solution to return a json with the Magento\Framework\Webapi\Rest\Response class.

Try this:

$result = json_encode(['message' => 'test']);
return $this->response->setBody($result)->sendResponse();

Your Answer

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

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