Magento 2 JSON encode or decode
In Magento 2, JSON serialization and unserialization can be performed using the \Magento\Framework\Serialize\Serializer\Json class. This class provides methods to serialize data to JSON format and unserialize JSON-encoded data back to its original PHP representation.
Here's an example of how you can encode or decode JSON data in Magento 2.
saveCopyzoom_out_mapuse Magento\Framework\Serialize\Serializer\Json;
class YourClass
{
protected $jsonSerializer;
public function __construct(
Json $jsonSerializer
) {
$this->jsonSerializer = $jsonSerializer;
}
public function serializeData(array $data)
{
// Serialize data to JSON format
$serializedData = $this->jsonSerializer->serialize($data);
return $serializedData;
}
public function unserializeData(string $jsonData)
{
// Unserialize JSON-encoded data
$unserializedData = $this->jsonSerializer->unserialize($jsonData);
return $unserializedData;
}
}
As we know magento 2 deprecated
Magento\Framework\Json\Helper\Dataclass but introduces the new classMagento\Framework\Serialize\Serializer\Jsonfor Json encode or decode.
We've injected new class Magento\Framework\Serialize\Serializer\Json in the above code. You can use the above created methods serializeData and unserializeData for Json encode or decode in Magento 2. Check the below example code for your reference.
saveCopyzoom_out_map$jsonData = '{"key1": "value1", "key2": "value2"}'; // Serialize data $serialized = $youClassInstance->serializeData(['key1' => 'value1', 'key2' => 'value2']); echo "Serialized data: " . $serialized . PHP_EOL; // Unserialize data $unserialized = $youClassInstance->unserializeData($jsonData); echo "Unserialized data: "; print_r($unserialized);
Replace YourClass with the appropriate class where you're implementing serialization and unserialization. The Json class from Magento\Framework\Serialize\Serializer\Json is utilized for this purpose.
Remember to inject the Json serializer in your class constructor via dependency injection to use it within your custom class or block. This way, you can easily serialize and unserialize data using Magento 2's built-in JSON serialization methods.
We hope this article helped you to know How to encode or decode Json data in Magento 2.
Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this post with your team.
AI-Powered Recommended Articles
How to create JSON controller in Magento 2
Learn how to create a JSON controller in Magento for handling AJAX requests and returning JSON data.
Magento 2 Add Customer Attribute Programmatically
Learn how to add custom customer attributes programmatically in Magento 2 for customized customer data.
Magento 2 Remove Product Attribute Programmatically
Learn how to remove product attributes programmatically in Magento 2 to streamline product management.
How to Get Salable Quantity in Magento 2
Learn how to retrieve the salable quantity of a product in Magento 2 to ensure accurate inventory tracking and display on your site.
Magento 2 Create Category Attribute Programmatically
Learn how to create custom category attributes programmatically in Magento 2 for advanced category configurations.
Magento 2 Add Product Attribute Programmatically
Learn how to programmatically add product attributes in Magento 2 for enhanced product management.