Problem Summary: You have a PHP Array and you want to return it as a JSON Object. When you return your PHP Array, it gets converted to a JSON Array, which does not support non-sequential keys. So your keys get ignored and you end up with a sequential array of your values.
Solution Summary: You need to return a PHP Object in order to have a JSON Object in your response.
Solution Example
Please check following example that returns a sample Components object.
/Api/Data/ComponentsInterface.php
<?php
namespace Company\Components\Api\Data;
/**
* Interface ComponentsInterface
*/
interface ComponentsInterface
{
/**
* Get component 1
*
* @return string
*/
public function getComponent1();
/**
* Get component 2
*
* @return string
*/
public function getComponent2();
}
/Api/ComponentsManagamentInterface.php
<?php
namespace Company\Components\Api;
/**
* Components Management interface
*/
interface ComponentsManagementInterface
{
/**
* @return \Company\Components\Api\Data\ComponentsInterface
*/
public function getComponents();
}
/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/components" method="GET">
<service class="Company\Components\Api\ComponentsManagementInterface" method="getComponents"/>
</route>
</routes>
/Model/Components.php
<?php
namespace Company\Components\Model;
/**
* Components model
*/
class Components extends \Magento\Framework\DataObject implements \Company\Component\Api\Data\ComponentsInterface
{
/**
* @inheritDoc
*/
public function getComponent1()
{
return $this->getData('component1');
}
/**
* @inheritDoc
*/
public function getComponent2()
{
return $this->getData('component2');
}
}
/Model/ComponentsManagement.php
<?php
namespace Company\Components\Model;
/**
* Components Management
*/
class ComponentsManagement implements \Company\Components\Api\ComponentsManagementInterface
{
/**
* @var \Company\Components\Api\Data\ComponentsInterfaceFactory
*/
protected $_componentsFactory;
/**
* ComponentsManagement constructor.
* @param \Company\Components\Api\Data\ComponentsInterfaceFactory $_componentsFactory
*/
public function __construct(\Company\Components\Api\Data\ComponentsInterfaceFactory $_componentsFactory)
{
$this->_componentsFactory = $_componentsFactory;
}
/**
* @inheritDoc
*/
public function getComponents()
{
$components = $this->_componentsFactory->create();
$components->setData([
'component1' => '[{....}]',
'component2' => '[{....}]'
]);
return $components;
}
}
/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Company\Components\Api\Data\ComponentsInterface"
type="Company\Components\Model\Components"/>
<preference for="Company\Components\Api\ComponentsManagementInterface"
type="Company\Components\Model\ComponentsManagement"/>
</config>
With above example, when you GET
http://magento/rest/V1/components
it will return following JSON Object:
{"component_1":"[{....}]","component_2":"[{....}]"}