Your module adds a new controller class which will return a JSON response.
What will be the return type of the execute method?
A. You should implement a new API endpoint instead of returning JSON from a controller
B. The string value of Zend_Json::encode()
C. An instance of MagentoFrameworkControllerResultJson
D. No return needed, an object that can be converted to JSON must be set as the Response body
Answer: C
Allowed return types and their classes
/**#@-*/
protected $typeMap = [
self::TYPE_JSON => \Magento\Framework\Controller\Result\Json::class,
self::TYPE_RAW => \Magento\Framework\Controller\Result\Raw::class,
self::TYPE_REDIRECT => \Magento\Framework\Controller\Result\Redirect::class,
self::TYPE_FORWARD => \Magento\Framework\Controller\Result\Forward::class,
self::TYPE_LAYOUT => \Magento\Framework\View\Result\Layout::class,
self::TYPE_PAGE => \Magento\Framework\View\Result\Page::class,
];
from \Magento\Framework\Controller\ResultFactory
Answer:- C
return types
A “MagentoFrameworkControllerResultJson” result
A “MagentoFrameworkControllerResultRaw” result
A “MagentoFrameworkControllerResultForward” result
A “MagentoFrameworkControllerResultRedirect” result
from \Magento\Framework\Controller\ResultFactory
/**#@+
* Allowed result types
*/
const TYPE_JSON = ‘json’;
const TYPE_RAW = ‘raw’;
const TYPE_REDIRECT = ‘redirect’;
const TYPE_FORWARD = ‘forward’;
const TYPE_LAYOUT = ‘layout’;
const TYPE_PAGE = ‘page’;
Correct answer C
https://magento.stackexchange.com/questions/99358/magento2-how-to-get-json-response-from-controller