Magento2: How to convert JSON to Array of nested objects The 2019 Stack Overflow Developer Survey Results Are InProperly convert Magento's returned object to JSONMagento 2 Rest api POST JSON TypeProcessor errorMagento2: Unable to post 2D Array from REST ApiMagento2 - A simple custom API moduleMagento 2 - Best approach for retrieving product data - REST API or Custom Endpoint?Error on image upload with REST apiMagento 2.2.2 REST API - I need to change the customer password using REST API“Class mixed does not exist” with only one array param in the body in Magento 2Add configure product in Cart using Magento 2 API facing an issueMagento2 REST API get all customers details

Match Roman Numerals

Can there be female White Walkers?

Straighten subgroup lattice

"as much details as you can remember"

Deal with toxic manager when you can't quit

Why not take a picture of a closer black hole?

How to type a long/em dash `—`

How can I define good in a religion that claims no moral authority?

How to translate "being like"?

What does Linus Torvalds mean when he says that Git "never ever" tracks a file?

Cooking pasta in a water boiler

What do hard-Brexiteers want with respect to the Irish border?

Is Cinnamon a desktop environment or a window manager? (Or both?)

How to support a colleague who finds meetings extremely tiring?

What information about me do stores get via my credit card?

Why doesn't UInt have a toDouble()?

What is the meaning of Triage in Cybersec world?

Keeping a retro style to sci-fi spaceships?

Worn-tile Scrabble

If I can cast sorceries at instant speed, can I use sorcery-speed activated abilities at instant speed?

ODD NUMBER in Cognitive Linguistics of WILLIAM CROFT and D. ALAN CRUSE

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Is it okay to consider publishing in my first year of PhD?

Can a flute soloist sit?



Magento2: How to convert JSON to Array of nested objects



The 2019 Stack Overflow Developer Survey Results Are InProperly convert Magento's returned object to JSONMagento 2 Rest api POST JSON TypeProcessor errorMagento2: Unable to post 2D Array from REST ApiMagento2 - A simple custom API moduleMagento 2 - Best approach for retrieving product data - REST API or Custom Endpoint?Error on image upload with REST apiMagento 2.2.2 REST API - I need to change the customer password using REST API“Class mixed does not exist” with only one array param in the body in Magento 2Add configure product in Cart using Magento 2 API facing an issueMagento2 REST API get all customers details



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








2















I am wanting to understand if there are some functions in Magento Framework which will assist me in mapping an array of nested objects from JSON (such as what would happen when the REST API is building objects from JSON - although in this case I'll be loading from code.)



My structure would be something like this (array of parent_objects)



{"parent_object":[
"id":0, "product""sku":"prod2", "name":"prod2",
"id":0, "product""sku":"prod3", "name":"prod3", "customobject":"field1":"field1-value"
]



And the class I would have the data mapped to is an array of:



MyNsMymoduleApiDataParentObjectDataInterface[] $parentObject


which contains these three properties (mix of primitive and object types)



 @var MagentoCatalogApiDataProductInterface $product
@var int $id
@var MyNsMymoduleApiDataCustomobjectInterface $customobject


Thanks.



EDIT: Currently trying to trace the process:



Appears that in MagentoFrameworkWebapiRestRequestDeserializerJson we have a method deserialize($encodedBody) that converts JSON to Array.



Then this is used in MagentoFrameworkWebapiRestRequest method getBodyParams()



Still tracing my way back now - I've also looked at the reflection classes, don't want to complicate too much and am looking for a solid answer where someone knows the best way.










share|improve this question






























    2















    I am wanting to understand if there are some functions in Magento Framework which will assist me in mapping an array of nested objects from JSON (such as what would happen when the REST API is building objects from JSON - although in this case I'll be loading from code.)



    My structure would be something like this (array of parent_objects)



    {"parent_object":[
    "id":0, "product""sku":"prod2", "name":"prod2",
    "id":0, "product""sku":"prod3", "name":"prod3", "customobject":"field1":"field1-value"
    ]



    And the class I would have the data mapped to is an array of:



    MyNsMymoduleApiDataParentObjectDataInterface[] $parentObject


    which contains these three properties (mix of primitive and object types)



     @var MagentoCatalogApiDataProductInterface $product
    @var int $id
    @var MyNsMymoduleApiDataCustomobjectInterface $customobject


    Thanks.



    EDIT: Currently trying to trace the process:



    Appears that in MagentoFrameworkWebapiRestRequestDeserializerJson we have a method deserialize($encodedBody) that converts JSON to Array.



    Then this is used in MagentoFrameworkWebapiRestRequest method getBodyParams()



    Still tracing my way back now - I've also looked at the reflection classes, don't want to complicate too much and am looking for a solid answer where someone knows the best way.










    share|improve this question


























      2












      2








      2


      1






      I am wanting to understand if there are some functions in Magento Framework which will assist me in mapping an array of nested objects from JSON (such as what would happen when the REST API is building objects from JSON - although in this case I'll be loading from code.)



      My structure would be something like this (array of parent_objects)



      {"parent_object":[
      "id":0, "product""sku":"prod2", "name":"prod2",
      "id":0, "product""sku":"prod3", "name":"prod3", "customobject":"field1":"field1-value"
      ]



      And the class I would have the data mapped to is an array of:



      MyNsMymoduleApiDataParentObjectDataInterface[] $parentObject


      which contains these three properties (mix of primitive and object types)



       @var MagentoCatalogApiDataProductInterface $product
      @var int $id
      @var MyNsMymoduleApiDataCustomobjectInterface $customobject


      Thanks.



      EDIT: Currently trying to trace the process:



      Appears that in MagentoFrameworkWebapiRestRequestDeserializerJson we have a method deserialize($encodedBody) that converts JSON to Array.



      Then this is used in MagentoFrameworkWebapiRestRequest method getBodyParams()



      Still tracing my way back now - I've also looked at the reflection classes, don't want to complicate too much and am looking for a solid answer where someone knows the best way.










      share|improve this question
















      I am wanting to understand if there are some functions in Magento Framework which will assist me in mapping an array of nested objects from JSON (such as what would happen when the REST API is building objects from JSON - although in this case I'll be loading from code.)



      My structure would be something like this (array of parent_objects)



      {"parent_object":[
      "id":0, "product""sku":"prod2", "name":"prod2",
      "id":0, "product""sku":"prod3", "name":"prod3", "customobject":"field1":"field1-value"
      ]



      And the class I would have the data mapped to is an array of:



      MyNsMymoduleApiDataParentObjectDataInterface[] $parentObject


      which contains these three properties (mix of primitive and object types)



       @var MagentoCatalogApiDataProductInterface $product
      @var int $id
      @var MyNsMymoduleApiDataCustomobjectInterface $customobject


      Thanks.



      EDIT: Currently trying to trace the process:



      Appears that in MagentoFrameworkWebapiRestRequestDeserializerJson we have a method deserialize($encodedBody) that converts JSON to Array.



      Then this is used in MagentoFrameworkWebapiRestRequest method getBodyParams()



      Still tracing my way back now - I've also looked at the reflection classes, don't want to complicate too much and am looking for a solid answer where someone knows the best way.







      magento2 json object mapping






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 12 at 11:36









      Aasim Goriya

      2,9261939




      2,9261939










      asked May 18 '17 at 7:22









      BAFBAF

      461415




      461415




















          3 Answers
          3






          active

          oldest

          votes


















          2














          Use jsonDecode() function of helper MagentoFrameworkJsonHelperData



          protected $jsonHelper;

          public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)

          $this->jsonHelper = $jsonHelper;


          public function decodeSomething($jsonData)

          return $this->jsonHelper->jsonDecode($jsonData);






          share|improve this answer

























          • Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

            – BAF
            May 18 '17 at 7:47



















          1














          Found how to achieve this:



          Use the following to convert the JSON into an array:



          MagentoFrameworkWebapiRestRequestDeserializerJsonDeserializer::deserialize('jsonString')


          Use the following to convert to an object (Path to class is the class that the object will be processed by)



          MagentoFrameworkWebapiServiceInputProcessor::process('PathToClass', 'methodName', $deserializedJson)





          share|improve this answer






























            1














            MagentoFrameworkJsonHelperData is deprecated since Magento 100.2.0



            You could use MagentoFrameworkSerializeSerializerJson instead.



            Ie.



             public function __construct(
            MagentoFrameworkSerializeSerializerJson $json
            )
            $this->json = $json;


            public function yourFunction()

            $jsonDecode = $this->json->unserialize($result);

            $json = $this->json->serialize(jsonDecode);







            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%2f174975%2fmagento2-how-to-convert-json-to-array-of-nested-objects%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









              2














              Use jsonDecode() function of helper MagentoFrameworkJsonHelperData



              protected $jsonHelper;

              public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)

              $this->jsonHelper = $jsonHelper;


              public function decodeSomething($jsonData)

              return $this->jsonHelper->jsonDecode($jsonData);






              share|improve this answer

























              • Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

                – BAF
                May 18 '17 at 7:47
















              2














              Use jsonDecode() function of helper MagentoFrameworkJsonHelperData



              protected $jsonHelper;

              public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)

              $this->jsonHelper = $jsonHelper;


              public function decodeSomething($jsonData)

              return $this->jsonHelper->jsonDecode($jsonData);






              share|improve this answer

























              • Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

                – BAF
                May 18 '17 at 7:47














              2












              2








              2







              Use jsonDecode() function of helper MagentoFrameworkJsonHelperData



              protected $jsonHelper;

              public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)

              $this->jsonHelper = $jsonHelper;


              public function decodeSomething($jsonData)

              return $this->jsonHelper->jsonDecode($jsonData);






              share|improve this answer















              Use jsonDecode() function of helper MagentoFrameworkJsonHelperData



              protected $jsonHelper;

              public function __construct(MagentoFrameworkJsonHelperData $jsonHelper)

              $this->jsonHelper = $jsonHelper;


              public function decodeSomething($jsonData)

              return $this->jsonHelper->jsonDecode($jsonData);







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 12 at 11:33

























              answered May 18 '17 at 7:37









              Prince PatelPrince Patel

              14.6k65481




              14.6k65481












              • Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

                – BAF
                May 18 '17 at 7:47


















              • Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

                – BAF
                May 18 '17 at 7:47

















              Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

              – BAF
              May 18 '17 at 7:47






              Hi Prince Patel, this would only turn it into a standard object or array - i want something like is used in the REST API to decode into the actual type that I will pass through in the method used to work with this data - basically an object array of ParentObject - including actual sub objects of the particular type that this ParentObject has as properties.

              – BAF
              May 18 '17 at 7:47














              1














              Found how to achieve this:



              Use the following to convert the JSON into an array:



              MagentoFrameworkWebapiRestRequestDeserializerJsonDeserializer::deserialize('jsonString')


              Use the following to convert to an object (Path to class is the class that the object will be processed by)



              MagentoFrameworkWebapiServiceInputProcessor::process('PathToClass', 'methodName', $deserializedJson)





              share|improve this answer



























                1














                Found how to achieve this:



                Use the following to convert the JSON into an array:



                MagentoFrameworkWebapiRestRequestDeserializerJsonDeserializer::deserialize('jsonString')


                Use the following to convert to an object (Path to class is the class that the object will be processed by)



                MagentoFrameworkWebapiServiceInputProcessor::process('PathToClass', 'methodName', $deserializedJson)





                share|improve this answer

























                  1












                  1








                  1







                  Found how to achieve this:



                  Use the following to convert the JSON into an array:



                  MagentoFrameworkWebapiRestRequestDeserializerJsonDeserializer::deserialize('jsonString')


                  Use the following to convert to an object (Path to class is the class that the object will be processed by)



                  MagentoFrameworkWebapiServiceInputProcessor::process('PathToClass', 'methodName', $deserializedJson)





                  share|improve this answer













                  Found how to achieve this:



                  Use the following to convert the JSON into an array:



                  MagentoFrameworkWebapiRestRequestDeserializerJsonDeserializer::deserialize('jsonString')


                  Use the following to convert to an object (Path to class is the class that the object will be processed by)



                  MagentoFrameworkWebapiServiceInputProcessor::process('PathToClass', 'methodName', $deserializedJson)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 25 '17 at 17:27









                  BAFBAF

                  461415




                  461415





















                      1














                      MagentoFrameworkJsonHelperData is deprecated since Magento 100.2.0



                      You could use MagentoFrameworkSerializeSerializerJson instead.



                      Ie.



                       public function __construct(
                      MagentoFrameworkSerializeSerializerJson $json
                      )
                      $this->json = $json;


                      public function yourFunction()

                      $jsonDecode = $this->json->unserialize($result);

                      $json = $this->json->serialize(jsonDecode);







                      share|improve this answer



























                        1














                        MagentoFrameworkJsonHelperData is deprecated since Magento 100.2.0



                        You could use MagentoFrameworkSerializeSerializerJson instead.



                        Ie.



                         public function __construct(
                        MagentoFrameworkSerializeSerializerJson $json
                        )
                        $this->json = $json;


                        public function yourFunction()

                        $jsonDecode = $this->json->unserialize($result);

                        $json = $this->json->serialize(jsonDecode);







                        share|improve this answer

























                          1












                          1








                          1







                          MagentoFrameworkJsonHelperData is deprecated since Magento 100.2.0



                          You could use MagentoFrameworkSerializeSerializerJson instead.



                          Ie.



                           public function __construct(
                          MagentoFrameworkSerializeSerializerJson $json
                          )
                          $this->json = $json;


                          public function yourFunction()

                          $jsonDecode = $this->json->unserialize($result);

                          $json = $this->json->serialize(jsonDecode);







                          share|improve this answer













                          MagentoFrameworkJsonHelperData is deprecated since Magento 100.2.0



                          You could use MagentoFrameworkSerializeSerializerJson instead.



                          Ie.



                           public function __construct(
                          MagentoFrameworkSerializeSerializerJson $json
                          )
                          $this->json = $json;


                          public function yourFunction()

                          $jsonDecode = $this->json->unserialize($result);

                          $json = $this->json->serialize(jsonDecode);








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 8 at 10:59









                          diazwatsondiazwatson

                          1,59811425




                          1,59811425



























                              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%2f174975%2fmagento2-how-to-convert-json-to-array-of-nested-objects%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

                              Sum ergo cogito? 1 nng

                              419 nièngy_Soadمي 19bal1.5o_g

                              Queiggey Chernihivv 9NnOo i Zw X QqKk LpB