I am wanting to understand if there are some functions in Magento Framework which will assist me in mapping an array of nested objects from JSON (such as what would happen when the REST API is building objects from JSON - although in this case I'll be loading from code.)

My structure would be something like this (array of parent_objects)

      {"id":0, "product"{"sku":"prod2", "name":"prod2"},
      {"id":0, "product"{"sku":"prod3", "name":"prod3"}, "customobject":{"field1":"field1-value"}}

And the class I would have the data mapped to is an array of:

\MyNs\Mymodule\Api\Data\ParentObjectDataInterface[] $parentObject

which contains these three properties (mix of primitive and object types)

 @var \Magento\Catalog\Api\Data\ProductInterface $product
 @var int $id
 @var \MyNs\Mymodule\Api\Data\CustomobjectInterface $customobject


EDIT: Currently trying to trace the process:

Appears that in \Magento\Framework\Webapi\Rest\Request\Deserializer\Json we have a method deserialize($encodedBody) that converts JSON to Array.

Then this is used in \Magento\Framework\Webapi\Rest\Request method getBodyParams()

Still tracing my way back now - I've also looked at the reflection classes, don't want to complicate too much and am looking for a solid answer where someone knows the best way.

2 Answers 2


\Magento\Framework\Json\Helper\Data is deprecated since Magento 100.2.0

You could use \Magento\Framework\Serialize\Serializer\Json instead.


 public function __construct(
        \Magento\Framework\Serialize\Serializer\Json $json
    ) {
        $this->json = $json;

 public function yourFunction()
   $jsonDecode = $this->json->unserialize($result);

   $json = $this->json->serialize($jsonDecode);


Found how to achieve this:

Use the following to convert the JSON into an array:


Use the following to convert to an object (Path to class is the class that the object will be processed by)

\Magento\Framework\Webapi\ServiceInputProcessor::process('\Path\To\Class', 'methodName', $deserializedJson)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.