2

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)

{"parent_object":[
      {"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

Thanks.

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

Use jsonDecode() function of helper Magento\Framework\Json\Helper\Data

protected $jsonHelper;

public function __construct(\Magento\Framework\Json\Helper\Data $jsonHelper)
{
    $this->jsonHelper = $jsonHelper;
}

public function decodeSomething($jsonData)
{
    return $this->jsonHelper->jsonDecode($jsonData);
}
  • Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties. – BAF May 18 '17 at 7:47
2

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

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

Ie.

 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);

 }
1

Found how to achieve this:

Use the following to convert the JSON into an array:

\Magento\Framework\Webapi\Rest\Request\Deserializer\Json\Deserializer::deserialize('jsonString')

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, privacy policy and cookie policy

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