Magento – how to get mixed or multi array response in rest API magento2

apimagento-2.0magento2rest

i have my custom api which will return multi dimension array , which will have key pair value, where key and value are dynamic from database in that case how to achieve in rest api of magento2

Best Answer

to achieve multi dimension array where key and value are dynamic we can follow below steps but magento discourage this solution if any better solution please let me know out put

{
"name": "Test Name"
"key": "value"
"key2": "value2"
"multi": {
"name": "Test Name"
"key": "value"
"key2": "value2"
}-
}

where key and value are from db or dynamic

to create custom api follow my post in How do I get a response from REST API in JSON format in Magento 2?

and do the below changes

in di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

    <preference for="Magento\Framework\Webapi\ServiceOutputProcessor" type="Sugarcode\Customapi\Model\ServiceOutputProcessor" />

    <preference for="Sugarcode\Customapi\Api\TestInterface"
                type="Sugarcode\Customapi\Model\Test" />

    <preference for="Sugarcode\Customapi\Api\Data\TestdataInterface" type="Sugarcode\Customapi\Model\Testmodel" />
</config>

app\code\Sugarcode\Customapi\Api\TestInterface.php

use @return array (which i not allowed in magento2 default ) that is

<?php


namespace Sugarcode\Customapi\Api;

use Sugarcode\Customapi\Api\Data\TestdataInterface;

interface TestInterface
{
    /**
     * Retrieve list of info
     *
     * @throws \Magento\Framework\Exception\NoSuchEntityException If ID is not found
     * @return array
     */
    public function getinfo();

}

app\code\Sugarcode\Customapi\Model\Test.php

public function getinfo() {
        $page_object = $this->dataFactory->create();
        $page_object->setName('eee');
        $page_object->setExtData(['key'=>'value', 'key2'=>'value2']);
        //return $page_object;
        return ['name'=>'Test Name','key'=>'value', 'key2'=>'value2',
            'multi'=>['name'=>'Test Name','key'=>'value', 'key2'=>'value2']
        ];      

    }

app\code\Sugarcode\Customapi\Model\ServiceOutputProcessor.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Sugarcode\Customapi\Model;;


/**
 * Data object converter for REST
 */
class ServiceOutputProcessor  extends \Magento\Framework\Webapi\ServiceOutputProcessor
{

     */
    public function process($data, $serviceClassName, $serviceMethodName)
    {
        /** @var string $dataType */
        $dataType = $this->methodsMapProcessor->getMethodReturnType($serviceClassName, $serviceMethodName);
        if($dataType == 'array'){
            return $data;           
        }else{
            return $this->convertValue($data, $dataType);

        }
    }


}

if any better solution guy please post

Related Topic