Existing controller that returns a JSON.

public function execute()

    $customer = $this->_customerModel->getById(175);
    $resultJson = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON);
            'status' => 'ok',
            'message' => 'Success.',
            'data' => $customer->__toArray()

    return $resultJson;

I have an afterExecute plugin where I try to get the data of the result

public function afterExecute(TestEndpoint $subject, $result)
    $result2 = json_decode($result);
    return $result;

There is an error saying thet $result is an Object not a JSON string. I want to get the customer data that was returned in the controller. How do I do that?

  • Plugins can not be used on Final methods,Final classes,Non-public methods,Class methods (such as static methods),__construct and __destruct,Virtual types,Objects that are instantiated before Magento\Framework\Interception is bootstrapped,Objects that implement Magento\Framework\ObjectManager\NoninterceptableInterface. Read devdocs.magento.com/guides/v2.4/extension-dev-guide/… Sep 15, 2021 at 5:16
  • are you looking for around plugin or after plugin? Because question title is different and descriptions is different. Sep 15, 2021 at 9:46
  • Updated title to After
    – V Salvador
    Sep 17, 2021 at 5:14
  • @VSalvador did you find the solution.
    – mobz
    Dec 20, 2021 at 7:22

1 Answer 1


In afterExecute $result is a json object if you want to get the data as an array you need to call getData() on $result to retrieve the data setted with setData() in the original function.

Since the value of $result->getData() will be an array you cannot use json_decode but can use json_encode to get the value as a json string.

  • 2
    The $result is an object of type Magento\Framework\Controller\Result\Json, it has setData() but no getData() even in the parent class
    – V Salvador
    Sep 17, 2021 at 5:20

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.