Convert model collection to json_encodeBenefits of Mage::getSingleton('core/resource_iterator')Fetch value...

Strange behavior in TikZ draw command

Why is participating in the European Parliamentary elections used as a threat?

Hashing password to increase entropy

Why can't I get pgrep output right to variable on bash script?

Not hide and seek

What is the period/term used describe Giuseppe Arcimboldo's style of painting?

Travelling in US for more than 90 days

Make a Bowl of Alphabet Soup

Why is implicit conversion not ambiguous for non-primitive types?

How do you justify more code being written by following clean code practices?

Writing in a Christian voice

Started in 1987 vs. Starting in 1987

What is the tangent at a sharp point on a curve?

Extract substring according to regexp with sed or grep

Why doesn't Gödel's incompleteness theorem apply to false statements?

Highest stage count that are used one right after the other?

Are hand made posters acceptable in Academia?

How to track Account Description field changes in Field history Tracking?

How to get directions in deep space?

Index matching algorithm without hash-based data structures?

"Marked down as someone wanting to sell shares." What does that mean?

Showing mass murder in a kid's book

Does capillary rise violate hydrostatic paradox?

How would a solely written language work mechanically



Convert model collection to json_encode


Benefits of Mage::getSingleton('core/resource_iterator')Fetch value from collection without foreach loopFilter Collection with result of previous collectionPut another collection as the values of “WHERE … IN (…)”Magento pagination not working with custom objectsWorking with collections, should I use function getData()Foreach Loop Pulling Wrong Value From CollectionMagento Iterator Vs Foreach Loop How to return an array?Unable to load the collection in DataProvider by matching values in arrayConvert(re-write) array_filter code into foreach loop













0















Currently I'm converting my collection data to json_encode like this:



$collections = $this->modelFactory->create()->getCollection()
->addFieldToFilter('status','1');
$result = array();
foreach($collections as $collection){
$result[] = $collection->getData();
}
return json_encode($result);


The problem is I need to loop for each collection to get the array data so that I can put it inside json_encode function, is there a way I can convert the collection result to json_encode without looping?










share|improve this question





























    0















    Currently I'm converting my collection data to json_encode like this:



    $collections = $this->modelFactory->create()->getCollection()
    ->addFieldToFilter('status','1');
    $result = array();
    foreach($collections as $collection){
    $result[] = $collection->getData();
    }
    return json_encode($result);


    The problem is I need to loop for each collection to get the array data so that I can put it inside json_encode function, is there a way I can convert the collection result to json_encode without looping?










    share|improve this question



























      0












      0








      0








      Currently I'm converting my collection data to json_encode like this:



      $collections = $this->modelFactory->create()->getCollection()
      ->addFieldToFilter('status','1');
      $result = array();
      foreach($collections as $collection){
      $result[] = $collection->getData();
      }
      return json_encode($result);


      The problem is I need to loop for each collection to get the array data so that I can put it inside json_encode function, is there a way I can convert the collection result to json_encode without looping?










      share|improve this question
















      Currently I'm converting my collection data to json_encode like this:



      $collections = $this->modelFactory->create()->getCollection()
      ->addFieldToFilter('status','1');
      $result = array();
      foreach($collections as $collection){
      $result[] = $collection->getData();
      }
      return json_encode($result);


      The problem is I need to loop for each collection to get the array data so that I can put it inside json_encode function, is there a way I can convert the collection result to json_encode without looping?







      magento2 collection model performance json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 6 mins ago









      ABHISHEK TRIPATHI

      1,9101726




      1,9101726










      asked Jan 3 '18 at 6:45









      nortonuser nortonuser

      381114




      381114






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Try this:



          $result = $collections->toArray();

          return json_encode($result);


          or



          $result = $collections->getData();
          return json_encode($result);





          share|improve this answer































            0














            You can simply get the collection data & encode it to json, please follow the below code.



            protected $jsonHelper;

            public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)
            {
            $this->jsonHelper = $jsonHelper;
            }

            ...

            public function encodeSomething()
            {
            $collections = $this->modelFactory->create()->getCollection()
            ->addFieldToFilter('status','1');
            $result = $collection->getData();
            return $this->jsonHelper->jsonEncode($result);
            }





            share|improve this answer























              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%2f207835%2fconvert-model-collection-to-json-encode%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              Try this:



              $result = $collections->toArray();

              return json_encode($result);


              or



              $result = $collections->getData();
              return json_encode($result);





              share|improve this answer




























                1














                Try this:



                $result = $collections->toArray();

                return json_encode($result);


                or



                $result = $collections->getData();
                return json_encode($result);





                share|improve this answer


























                  1












                  1








                  1







                  Try this:



                  $result = $collections->toArray();

                  return json_encode($result);


                  or



                  $result = $collections->getData();
                  return json_encode($result);





                  share|improve this answer













                  Try this:



                  $result = $collections->toArray();

                  return json_encode($result);


                  or



                  $result = $collections->getData();
                  return json_encode($result);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 '18 at 6:47









                  Nero PhungNero Phung

                  9041620




                  9041620

























                      0














                      You can simply get the collection data & encode it to json, please follow the below code.



                      protected $jsonHelper;

                      public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)
                      {
                      $this->jsonHelper = $jsonHelper;
                      }

                      ...

                      public function encodeSomething()
                      {
                      $collections = $this->modelFactory->create()->getCollection()
                      ->addFieldToFilter('status','1');
                      $result = $collection->getData();
                      return $this->jsonHelper->jsonEncode($result);
                      }





                      share|improve this answer




























                        0














                        You can simply get the collection data & encode it to json, please follow the below code.



                        protected $jsonHelper;

                        public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)
                        {
                        $this->jsonHelper = $jsonHelper;
                        }

                        ...

                        public function encodeSomething()
                        {
                        $collections = $this->modelFactory->create()->getCollection()
                        ->addFieldToFilter('status','1');
                        $result = $collection->getData();
                        return $this->jsonHelper->jsonEncode($result);
                        }





                        share|improve this answer


























                          0












                          0








                          0







                          You can simply get the collection data & encode it to json, please follow the below code.



                          protected $jsonHelper;

                          public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)
                          {
                          $this->jsonHelper = $jsonHelper;
                          }

                          ...

                          public function encodeSomething()
                          {
                          $collections = $this->modelFactory->create()->getCollection()
                          ->addFieldToFilter('status','1');
                          $result = $collection->getData();
                          return $this->jsonHelper->jsonEncode($result);
                          }





                          share|improve this answer













                          You can simply get the collection data & encode it to json, please follow the below code.



                          protected $jsonHelper;

                          public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)
                          {
                          $this->jsonHelper = $jsonHelper;
                          }

                          ...

                          public function encodeSomething()
                          {
                          $collections = $this->modelFactory->create()->getCollection()
                          ->addFieldToFilter('status','1');
                          $result = $collection->getData();
                          return $this->jsonHelper->jsonEncode($result);
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 10 mins ago









                          ABHISHEK TRIPATHIABHISHEK TRIPATHI

                          1,9101726




                          1,9101726






























                              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%2f207835%2fconvert-model-collection-to-json-encode%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)...

                              夢乃愛華...