Returning json data on magento 2.1 controller
<?php
// ... Controller.php
public function execute()
{
$result = ['message' => 'Success'];
$this->getResponse()->representJson(
// Use the method jsonEncode from Magento\Framework\Json\Helper\Data Helper
$this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode([
'result' => $result
])
);
return;
}