Magento 2: How to return a JSON object from the API?
I am assuming that AppFactory\Core\Api\SettingInterface::get()
is a REST endpoint. In that case in phpdoc comments you need to define what this will return. Magento REST handler will take that value and process it to remove all data that are unnecessary. What's left will be encoded into JSON so in javascript you can retrieve it as already proper JS hash and not json-encoded string.
The trick about those endpoint is that you need to define very precisely what will you return. Magento will not be able to process something as general as "array" where you will set whatever you like.
In your case, in order not to try playing with array of strings, it will be easier to create an interface that your endpoint will return.
<?php
namespace AppFactory\Core\Api;
/**
* @api
*/
interface SettingsInterface
{
/**
* @return Data\SettingsInterface
*/
public function get();
}
Now when you return an instance of an object implementing that interface Magento will read its phpdocs and will process their return values.
Now create a file in AppFactory\Core\Api\Data\SettingsInterface
as follows
<?php
namespace AppFactory\Core\Api\Data;
interface SettingsInterface
{
/**
* @return int[]
**/
public function getSettings();
/**
* @return string[]
**/
public function getExtra();
}
Now when you create actual class that will implement those 2 get methods and you will return it in AppFactory\Core\Api\SettingsInterface::get()
then magento will return something like
{
"settings": [1, 2, 5],
"extra": ["my","array","of","strings"]
}
If you want another level you need to create another interface which will keep settings
structure and add it as a return value for AppFactory\Core\Api\Data\SettingsInterface::getSettings()
.
If you need to have something that will be dynamic and you do not want or can't prepare this structure interface then you can try setting json-encoded string and place @return string
for any of the fields. This way however you will have to make sure to manually decode that string after receiving the response as then your response will look like this:
{
"settings": [1, 2, 5],
"extra": "{\"test\":\"string\",\"value\":8}"
}
and in order to use response.extra.test
you will have to first do response.extra = JSON.parse(response.extra);
manually
I've also faced this problem, and as an alternative to the solution @Zefiryn proposed, I have worked around it by enclosing the return data in an array (or two). Please consider the example below.
/**
* My function
*
* @return
*/
public function myFunction()
{
$searchCriteria = $this->_searchCriteriaBuilder->addFilter('is_filterable_in_grid',true,'eq')->create();
$productAttributes = $this->_productAttributeRepository->getList($searchCriteria)->getItems();
$productAttributesArray = [];
foreach ($productAttributes as $attribute) {
$productAttributesArray[$attribute->getAttributeCode()] = $this->convertAttributeToArray($attribute);
}
return [[
"attributes"=>$productAttributesArray,
"another_thing"=>["another_thing_2"=>"two"]
]];
}
private function convertAttributeToArray($attribute) {
return [
"id" => $attribute->getAttributeId(),
"code" => $attribute->getAttributeCode(),
"type" => $attribute->getBackendType(),
"name" => $attribute->getStoreLabel(),
"options" => $attribute->getSource()->getAllOptions(false)
];
}
Due to how Magento 2 allows arrays of mixed content as return values, more complex data structures can be embedded inside other arrays. The sample above yields the following JSON response (truncated for readability).
[
{
"attributes": {
"special_price": {
"id": "78",
"code": "special_price",
"type": "decimal",
"name": "Special Price",
"options": []
},
"cost": {
"id": "81",
"code": "cost",
"type": "decimal",
"name": "Cost",
"options": []
},
"another_thing": {
"another_thing_2": "two"
}
}
]
Enclosing it in a single layer removes the keys of the array, and without enclosing it in any array results in an error.
Understandably none of this is ideal, but this approach allows me to control the consistency in the returned data structure to a certain degree (the expected data structure and types). If you are also in control of writing a client-side library, an interceptor can be implemented to strip the outer array before returning it to the application.
I know this question is quite old, but there is one quite simple solution for this:
You either need to replace the Json-Renderer Magento\Framework\Webapi\Rest\Response\Renderer\Json
or you write a Plugin for it.
Here a little example of a plugin:
In your di.xml
<type name="Magento\Framework\Webapi\Rest\Response\Renderer\Json">
<plugin name="namespace_module_renderer_json_plugin" type="Namespace\Module\Plugin\Webapi\RestResponse\JsonPlugin" sortOrder="100" disabled="false" />
</type>
In your new Plugin-Class Namespace\Module\Plugin\Webapi\RestResponse\JsonPlugin
<?php
namespace Namespace\Module\Plugin\Webapi\RestResponse;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Framework\Webapi\Rest\Response\Renderer\Json;
class JsonPlugin
{
/** @var Request */
private $request;
/**
* JsonPlugin constructor.
* @param Request $request
*/
public function __construct(
Request $request
)
{
$this->request = $request;
}
/**
* @param Json $jsonRenderer
* @param callable $proceed
* @param $data
* @return mixed
*/
public function aroundRender(Json $jsonRenderer, callable $proceed, $data)
{
if ($this->request->getPathInfo() == "/V1/my/rest-route" && $this->isJson($data)) {
return $data;
}
return $proceed($data);
}
/**
* @param $data
* @return bool
*/
private function isJson($data)
{
if (!is_string($data)) {
return false;
}
json_decode($data);
return (json_last_error() == JSON_ERROR_NONE);
}
}
What happens here:
- If the rest-route is "/V1/my/rest-route", then the new rendering-method is used, which means simply, that the data is not encoded.
- An additional check-method is used to evaluate if the string is really a json-object. Otherwise (for instance, if the response is an 401-error, is would result in an internal error and give back a wrong status code)
- This way, in your rest-method, you can give back a json-string, which will not be changed.
Of course you can also write your own Renderer, which processes an array for instance.