magento 2 custom api return in json without response tag Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to return a JSON object with a custom REST API in Magento 2?Force JSON Encode to Return ObjectMagento 2: Intercept controller's JSON responseHow do I get a response from REST API in JSON format in Magento 2?Magento 2 API - declare @return JSONMagento 2 - How to return custom json response from APIREST API vs JSONCustom REST API returns duplicate “response” tagHow to find Magento version using API (JSON specifically)Refresh Cache via JSON APIMagento 2: Is there a way to return custom json response from REST API without building a data interface?

Simple Http Server

Asymptotics question

Printing attributes of selection in ArcPy?

Is it dangerous to install hacking tools on my private linux machine?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Delete free apps from library

What are the main differences between Stargate SG-1 cuts?

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

Where is the Next Backup Size entry on iOS 12?

How to write capital alpha?

Why does electrolysis of aqueous concentrated sodium bromide produce bromine at the anode?

Why is the change of basis formula counter-intuitive? [See details]

Sally's older brother

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

"klopfte jemand" or "jemand klopfte"?

RSA find public exponent

How to ternary Plot3D a function

Why is std::move not [[nodiscard]] in C++20?

One-one communication

Why is it faster to reheat something than it is to cook it?

After Sam didn't return home in the end, were he and Al still friends?

Is multiple magic items in one inherently imbalanced?

Caught masturbating at work

Random body shuffle every night—can we still function?



magento 2 custom api return in json without response tag



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to return a JSON object with a custom REST API in Magento 2?Force JSON Encode to Return ObjectMagento 2: Intercept controller's JSON responseHow do I get a response from REST API in JSON format in Magento 2?Magento 2 API - declare @return JSONMagento 2 - How to return custom json response from APIREST API vs JSONCustom REST API returns duplicate “response” tagHow to find Magento version using API (JSON specifically)Refresh Cache via JSON APIMagento 2: Is there a way to return custom json response from REST API without building a data interface?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have created a custom api following "inchoo" tutorial. But I am getting the data in string format instead of json.



From my browser I am getting a tag surrounding the api return as below.
enter image description here
But Chrome's postman extension showing a double quote ("") and some extra slashes () in the in the response even after header to "Accept":"application/json" and "Content-Type":"application/json".
enter image description here
I just want to receive the exact same string I am returning in my Model class.



 public function name() 
//return "Hello, raihanruhin";
$arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
return $arr;










share|improve this question














bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Where is the Inchoo tutorial?

    – Khoa TruongDinh
    May 17 '17 at 7:49











  • inchoo.net/magento/api-magento/magento-2-custom-api I couldn't add this link in the original post because I have added two image already. 2+ isn't allowed with <10 points

    – Raihan Ruhin
    May 17 '17 at 7:54


















0















I have created a custom api following "inchoo" tutorial. But I am getting the data in string format instead of json.



From my browser I am getting a tag surrounding the api return as below.
enter image description here
But Chrome's postman extension showing a double quote ("") and some extra slashes () in the in the response even after header to "Accept":"application/json" and "Content-Type":"application/json".
enter image description here
I just want to receive the exact same string I am returning in my Model class.



 public function name() 
//return "Hello, raihanruhin";
$arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
return $arr;










share|improve this question














bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Where is the Inchoo tutorial?

    – Khoa TruongDinh
    May 17 '17 at 7:49











  • inchoo.net/magento/api-magento/magento-2-custom-api I couldn't add this link in the original post because I have added two image already. 2+ isn't allowed with <10 points

    – Raihan Ruhin
    May 17 '17 at 7:54














0












0








0


1






I have created a custom api following "inchoo" tutorial. But I am getting the data in string format instead of json.



From my browser I am getting a tag surrounding the api return as below.
enter image description here
But Chrome's postman extension showing a double quote ("") and some extra slashes () in the in the response even after header to "Accept":"application/json" and "Content-Type":"application/json".
enter image description here
I just want to receive the exact same string I am returning in my Model class.



 public function name() 
//return "Hello, raihanruhin";
$arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
return $arr;










share|improve this question














I have created a custom api following "inchoo" tutorial. But I am getting the data in string format instead of json.



From my browser I am getting a tag surrounding the api return as below.
enter image description here
But Chrome's postman extension showing a double quote ("") and some extra slashes () in the in the response even after header to "Accept":"application/json" and "Content-Type":"application/json".
enter image description here
I just want to receive the exact same string I am returning in my Model class.



 public function name() 
//return "Hello, raihanruhin";
$arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
return $arr;







magento2 api json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 17 '17 at 7:39









Raihan RuhinRaihan Ruhin

12




12





bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 1 min ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Where is the Inchoo tutorial?

    – Khoa TruongDinh
    May 17 '17 at 7:49











  • inchoo.net/magento/api-magento/magento-2-custom-api I couldn't add this link in the original post because I have added two image already. 2+ isn't allowed with <10 points

    – Raihan Ruhin
    May 17 '17 at 7:54


















  • Where is the Inchoo tutorial?

    – Khoa TruongDinh
    May 17 '17 at 7:49











  • inchoo.net/magento/api-magento/magento-2-custom-api I couldn't add this link in the original post because I have added two image already. 2+ isn't allowed with <10 points

    – Raihan Ruhin
    May 17 '17 at 7:54

















Where is the Inchoo tutorial?

– Khoa TruongDinh
May 17 '17 at 7:49





Where is the Inchoo tutorial?

– Khoa TruongDinh
May 17 '17 at 7:49













inchoo.net/magento/api-magento/magento-2-custom-api I couldn't add this link in the original post because I have added two image already. 2+ isn't allowed with <10 points

– Raihan Ruhin
May 17 '17 at 7:54






inchoo.net/magento/api-magento/magento-2-custom-api I couldn't add this link in the original post because I have added two image already. 2+ isn't allowed with <10 points

– Raihan Ruhin
May 17 '17 at 7:54











3 Answers
3






active

oldest

votes


















0














Look how Magento 2 handles api response..



class MagentoFrameworkWebapiServiceOutputProcessor {
...
public function convertValue($data, $type)

if (is_array($data))
$result = [];
$arrayElementType = substr($type, 0, -2);
foreach ($data as $datum)
if (is_object($datum))
$datum = $this->processDataObject(
$this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
);

$result[] = $datum;

return $result;
elseif ...



Magento 2 accept array type to response.



$arr = [
['name' => 'MasterCard'],
['name' => 'VISA'],
['name' => 'DBBL-NEXUS'],
['name' => 'American Express'],
];
return $arr;


or just..



$arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
return json_decode($arr, true);





share|improve this answer






























    0














    As I noted in this answer: https://magento.stackexchange.com/a/229971/2392, once you have the response string, you can tell PHP to convert it to a JSON object using this snippet:



    $object = json_decode( $response, false, 512, JSON_FORCE_OBJECT);


    Where $response is the string from your API response.



    Remember that variables only ever exist in the memory of the program you are executing. HTTP is a transport protocol, not a program, so it can only be used to send scalar (simple) variable types (e.g. String, integer, boolean) not complex types (like objects).



    So before an object can be sent over HTTP, a string representation of it must be created. This process is called serialisation (or serialization in US English).






    share|improve this answer






























      -1














      Modify your return line to:



      return json_decode(json_encode($arr), true);





      share|improve this answer























      • That will return a PHP associative array, not a string, which is what the OP appears to want

        – ProcessEight
        Jun 18 '18 at 11:47











      • The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

        – noctufaber
        Jun 19 '18 at 13:19











      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "479"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f174788%2fmagento-2-custom-api-return-in-json-without-response-tag%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Look how Magento 2 handles api response..



      class MagentoFrameworkWebapiServiceOutputProcessor {
      ...
      public function convertValue($data, $type)

      if (is_array($data))
      $result = [];
      $arrayElementType = substr($type, 0, -2);
      foreach ($data as $datum)
      if (is_object($datum))
      $datum = $this->processDataObject(
      $this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
      );

      $result[] = $datum;

      return $result;
      elseif ...



      Magento 2 accept array type to response.



      $arr = [
      ['name' => 'MasterCard'],
      ['name' => 'VISA'],
      ['name' => 'DBBL-NEXUS'],
      ['name' => 'American Express'],
      ];
      return $arr;


      or just..



      $arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
      return json_decode($arr, true);





      share|improve this answer



























        0














        Look how Magento 2 handles api response..



        class MagentoFrameworkWebapiServiceOutputProcessor {
        ...
        public function convertValue($data, $type)

        if (is_array($data))
        $result = [];
        $arrayElementType = substr($type, 0, -2);
        foreach ($data as $datum)
        if (is_object($datum))
        $datum = $this->processDataObject(
        $this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
        );

        $result[] = $datum;

        return $result;
        elseif ...



        Magento 2 accept array type to response.



        $arr = [
        ['name' => 'MasterCard'],
        ['name' => 'VISA'],
        ['name' => 'DBBL-NEXUS'],
        ['name' => 'American Express'],
        ];
        return $arr;


        or just..



        $arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
        return json_decode($arr, true);





        share|improve this answer

























          0












          0








          0







          Look how Magento 2 handles api response..



          class MagentoFrameworkWebapiServiceOutputProcessor {
          ...
          public function convertValue($data, $type)

          if (is_array($data))
          $result = [];
          $arrayElementType = substr($type, 0, -2);
          foreach ($data as $datum)
          if (is_object($datum))
          $datum = $this->processDataObject(
          $this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
          );

          $result[] = $datum;

          return $result;
          elseif ...



          Magento 2 accept array type to response.



          $arr = [
          ['name' => 'MasterCard'],
          ['name' => 'VISA'],
          ['name' => 'DBBL-NEXUS'],
          ['name' => 'American Express'],
          ];
          return $arr;


          or just..



          $arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
          return json_decode($arr, true);





          share|improve this answer













          Look how Magento 2 handles api response..



          class MagentoFrameworkWebapiServiceOutputProcessor {
          ...
          public function convertValue($data, $type)

          if (is_array($data))
          $result = [];
          $arrayElementType = substr($type, 0, -2);
          foreach ($data as $datum)
          if (is_object($datum))
          $datum = $this->processDataObject(
          $this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType)
          );

          $result[] = $datum;

          return $result;
          elseif ...



          Magento 2 accept array type to response.



          $arr = [
          ['name' => 'MasterCard'],
          ['name' => 'VISA'],
          ['name' => 'DBBL-NEXUS'],
          ['name' => 'American Express'],
          ];
          return $arr;


          or just..



          $arr = '["name":"MasterCard","name":"VISA", "name":"DBBL-NEXUS", "name":"American Express"]';
          return json_decode($arr, true);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 8 '17 at 22:31









          Nicholas MillerNicholas Miller

          863313




          863313























              0














              As I noted in this answer: https://magento.stackexchange.com/a/229971/2392, once you have the response string, you can tell PHP to convert it to a JSON object using this snippet:



              $object = json_decode( $response, false, 512, JSON_FORCE_OBJECT);


              Where $response is the string from your API response.



              Remember that variables only ever exist in the memory of the program you are executing. HTTP is a transport protocol, not a program, so it can only be used to send scalar (simple) variable types (e.g. String, integer, boolean) not complex types (like objects).



              So before an object can be sent over HTTP, a string representation of it must be created. This process is called serialisation (or serialization in US English).






              share|improve this answer



























                0














                As I noted in this answer: https://magento.stackexchange.com/a/229971/2392, once you have the response string, you can tell PHP to convert it to a JSON object using this snippet:



                $object = json_decode( $response, false, 512, JSON_FORCE_OBJECT);


                Where $response is the string from your API response.



                Remember that variables only ever exist in the memory of the program you are executing. HTTP is a transport protocol, not a program, so it can only be used to send scalar (simple) variable types (e.g. String, integer, boolean) not complex types (like objects).



                So before an object can be sent over HTTP, a string representation of it must be created. This process is called serialisation (or serialization in US English).






                share|improve this answer

























                  0












                  0








                  0







                  As I noted in this answer: https://magento.stackexchange.com/a/229971/2392, once you have the response string, you can tell PHP to convert it to a JSON object using this snippet:



                  $object = json_decode( $response, false, 512, JSON_FORCE_OBJECT);


                  Where $response is the string from your API response.



                  Remember that variables only ever exist in the memory of the program you are executing. HTTP is a transport protocol, not a program, so it can only be used to send scalar (simple) variable types (e.g. String, integer, boolean) not complex types (like objects).



                  So before an object can be sent over HTTP, a string representation of it must be created. This process is called serialisation (or serialization in US English).






                  share|improve this answer













                  As I noted in this answer: https://magento.stackexchange.com/a/229971/2392, once you have the response string, you can tell PHP to convert it to a JSON object using this snippet:



                  $object = json_decode( $response, false, 512, JSON_FORCE_OBJECT);


                  Where $response is the string from your API response.



                  Remember that variables only ever exist in the memory of the program you are executing. HTTP is a transport protocol, not a program, so it can only be used to send scalar (simple) variable types (e.g. String, integer, boolean) not complex types (like objects).



                  So before an object can be sent over HTTP, a string representation of it must be created. This process is called serialisation (or serialization in US English).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 18 '18 at 11:44









                  ProcessEightProcessEight

                  7121417




                  7121417





















                      -1














                      Modify your return line to:



                      return json_decode(json_encode($arr), true);





                      share|improve this answer























                      • That will return a PHP associative array, not a string, which is what the OP appears to want

                        – ProcessEight
                        Jun 18 '18 at 11:47











                      • The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

                        – noctufaber
                        Jun 19 '18 at 13:19















                      -1














                      Modify your return line to:



                      return json_decode(json_encode($arr), true);





                      share|improve this answer























                      • That will return a PHP associative array, not a string, which is what the OP appears to want

                        – ProcessEight
                        Jun 18 '18 at 11:47











                      • The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

                        – noctufaber
                        Jun 19 '18 at 13:19













                      -1












                      -1








                      -1







                      Modify your return line to:



                      return json_decode(json_encode($arr), true);





                      share|improve this answer













                      Modify your return line to:



                      return json_decode(json_encode($arr), true);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 8 '17 at 19:58









                      noctufabernoctufaber

                      13




                      13












                      • That will return a PHP associative array, not a string, which is what the OP appears to want

                        – ProcessEight
                        Jun 18 '18 at 11:47











                      • The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

                        – noctufaber
                        Jun 19 '18 at 13:19

















                      • That will return a PHP associative array, not a string, which is what the OP appears to want

                        – ProcessEight
                        Jun 18 '18 at 11:47











                      • The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

                        – noctufaber
                        Jun 19 '18 at 13:19
















                      That will return a PHP associative array, not a string, which is what the OP appears to want

                      – ProcessEight
                      Jun 18 '18 at 11:47





                      That will return a PHP associative array, not a string, which is what the OP appears to want

                      – ProcessEight
                      Jun 18 '18 at 11:47













                      The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

                      – noctufaber
                      Jun 19 '18 at 13:19





                      The previous answers ahead of mine also have return values of PHP associative arrays. Additionally, the Magento core code given above in the answer before mine shows an array return value. Perhaps a return value of an array is more desirable than a string. As I recall my struggles with Magento from a year ago, I had a similar problem to the OP and the solution I gave solved my problem. So even though I've not given the response the OP requested, it may be the answer he needs.

                      – noctufaber
                      Jun 19 '18 at 13:19

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f174788%2fmagento-2-custom-api-return-in-json-without-response-tag%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                      Magento 2 disable Secret Key on URL's from terminal The Next CEO of Stack OverflowMagento 2 Shortcut/GUI tool to perform commandline tasks for windowsIn menu add configuration linkMagento oAuth : Generating access token and access secretMagento 2 security key issue in Third-Party API redirect URIPublic actions in admin controllersHow to Disable Cache in Custom WidgetURL Key not changing in Magento 2Product URL Key gets deleted when importing custom options - Magento 2Problem with reindex terminalMagento 2 - bin/magento Commands not working in Cpanel Terminal

                      Aasi (pallopeli) Navigointivalikko