magento 2 custom api return in json without response tag Planned maintenance scheduled April...

Project Euler #1 in C++

How to change the tick of the color bar legend to black

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

A term for a woman complaining about things/begging in a cute/childish way

Why is a lens darker than other ones when applying the same settings?

How can a team of shapeshifters communicate?

Is there public access to the Meteor Crater in Arizona?

How does light 'choose' between wave and particle behaviour?

what is the log of the PDF for a Normal Distribution?

A `coordinate` command ignored

What are the main differences between the original Stargate SG-1 and the Final Cut edition?

Test print coming out spongy

RSA find public exponent

AppleTVs create a chatty alternate WiFi network

White walkers, cemeteries and wights

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

NERDTreeMenu Remapping

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

How many time has Arya actually used Needle?

Special flights

Putting class ranking in CV, but against dept guidelines

What would you call this weird metallic apparatus that allows you to lift people?

How were pictures turned from film to a big picture in a picture frame before digital scanning?



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 4 mins 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 4 mins 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 4 mins 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 4 mins 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

                      “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                      How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                      變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...