Magento 2 deprecated Magento\Framework\Json\Helper\Data but introduces new class Magento\Framework\Serialize\Serializer\Json for Json encode or decode.

Magento\Framework\Serialize\Serializer\Json have 2 object
 1. serialize() for Encode the Data like php function  jsonencode
 2. unserialize() for Decode the string like php function json
decode

namespace Hitesh\Vaghasiya\Controller;

class Index extends \Magento\Framework\App\Action\Action
{

   /**
    * @var \Magento\Framework\Serialize\Serializer\Json
    */
   protected $_json;

   /**
    * Index constructor.
    * @param \Magento\Framework\Serialize\Serializer\Json $json
    */
   public function __construct(
       \Magento\Framework\Serialize\Serializer\Json $json
   ) {
       $this->_json = $json;
   }

   /**
    * @param $data
    * @return bool|false|string
    */
   public function getJsonEncode($data)
   {
       return $this->_json->serialize($data); // it's same as like json_encode
   }

   /**
    * @param $data
    * @return array|bool|float|int|mixed|string|null
    */
   public function getJsonDecode($data)
   {
       return $this->_json->unserialize($data); // it's same as like json_decode
   }
}

Usering ObjectManager


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$jsoneObject = $objectManager->create('Magento\Framework\Serialize\Serializer\Json');

//json_encode
$array = array( "option1" => "data1", "option2" => "data2", ); 
$getJsonEncode = $jsoneObject->serialize($array); // it's same as like json_encode

//json_decode
$string = '{"option1":"data1","option2":"data2"}';
$getJsonDecode = $jsoneObject->unserialize($data); // it's same as like json_decode
Hitesh Vaghasiya

Hitesh Vaghasiya

Full stack Magento developer