Sometimes you may only have one action in the controller which is returned with the Json View of TYPO3 (for JavaScript, Ajax)
There is also a reasonable way to do this.
You create an initialize$replaceAction for each action and add the following code.
$this->defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class;
The biggest part is already done, please make sure that the desired value is output as return of your main method and put into the appropriate format with json_encode.
Have fun