4 Replies Latest reply on Oct 8, 2012 11:33 PM by Rafal Firlejczyk

    Need help with JSON returned by the service.

      I have created the sample "Store - StoreServer" application and it works fine on my machine.  I created my own service and wanted to use it.  In inspecting the JSON returned by my service and comparing it with the one returned by the store server application (URL: http://rhostore.heroku.com/products), I can find some differences.  The XML returned by both remain the same.  I have attached both the results below:

       

      Stream returned by rhostore URL:

      [{"product": {"name": "warszawska", "brand": "syrena", "updated_at": "2012-06-24T20:50:10Z", "price": "bezcenna", "quantity": null, "id": 87475, "sku": null, "created_at": "2012-06-24T20:50:10Z"}}, {"product": {"name": "ford", "brand": "ka", "updated_at": "2012-06-24T20:58:13Z", "price": "1", "quantity": null, "id": 87476, "sku": null, "created_at": "2012-06-24T20:58:13Z"}}, {"product": {"name": "toyota", "brand": "corola", "updated_at": "2012-06-24T21:46:51Z", "price": "6", "quantity": null, "id": 87477, "sku": null, "created_at": "2012-06-24T21:46:51Z"}}, {"product": {"name": "", "brand": "", "updated_at": "2012-06-25T04:48:37Z", "price": "", "quantity": "", "id": 87479, "sku": "", "created_at": "2012-06-25T04:48:37Z"}}, {"product": {"name": "krunal", "brand": "df", "updated_at": "2012-06-25T06:05:41Z", "price": "sdf", "quantity": "sdf", "id": 87480, "sku": "df", "created_at": "2012-06-25T04:59:29Z"}}, {"product": {"name": "ad", "brand": "adsf", "updated_at": "2012-06-25T06:08:33Z", "price": "asdf", "quantity": "sfsa", "id": 87478, "sku": "fsa", "created_at": "2012-06-25T04:47:54Z"}}, {"product": {"name": "asdfa", "brand": "asdf", "updated_at": "2012-06-25T06:08:52Z", "price": "dasf", "quantity": "asdf", "id": 87481, "sku": "sfasf", "created_at": "2012-06-25T06:08:32Z"}}, {"product": {"name": "rf", "brand": "f", "updated_at": "2012-06-25T06:28:59Z", "price": "f", "quantity": "ff", "id": 87482, "sku": "f", "created_at": "2012-06-25T06:28:59Z"}}, {"product": {"name": "Galaxy SIII", "brand": "Samsung", "updated_at": "2012-06-22T02:12:16Z", "price": "2300", "quantity": "", "id": 87464, "sku": "", "created_at": "2012-06-22T02:12:16Z"}}]

       

       

      Stream returned by my service:

       

      [{"brand":"brand
      0","id":"0","name":"Name
      0","price":"Price 0","quantity":"Qty
      0","sku":"Sku 0"},{"brand":"brand 1","id":"1","name":"Name
      1","price":"Price 1","quantity":"Qty
      1","sku":"Sku 1"},{"brand":"brand
      2","id":"2","name":"Name
      2","price":"Price 2","quantity":"Qty
      2","sku":"Sku 2"},{"brand":"brand
      3","id":"3","name":"Name
      3","price":"Price 3","quantity":"Qty
      3","sku":"Sku 3"},{"brand":"brand
      4","id":"4","name":"Name
      4","price":"Price 4","quantity":"Qty
      4","sku":"Sku 4"}]

       

      You can see that the phrase "product": is missing for each "record returned by the service.  Though there are workarounds, we would like to know what the real reason is. 

        • Re: Need help with JSON returned by the service.

          Your service returns array of hashes, and you can process your data by writing proper code in source adapter class:

           

          def query(params=nil)

              parsed=JSON.parse(RestClient.get("#{@base}.json").body)   

              @result={}

              parsed.each do |hash|

                  # process your hash here ...

              end if parsed   

              @result

          end

          • Re: Need help with JSON returned by the service.

            i had the same problem and i resolve it:

            indeed : @result[item["product"]["id"].to_s] = item["product"]

            i put: @result[item["id"].to_s] = item      (without : ["product"] ).

            • Re: Need help with JSON returned by the service.

              Syrena warszawska sounds like my father's first car!!

               

              You have two possibilities.

              1. You can adopt the output of your service to have in the json the name of the your model - here "product"

              or

              2. you can adopt the query in you source adapter of the rhoconnect server as it is written above - omit the "product"

               

              For point 1.:

              Assuming that you are running your service on the rails server you have to enable the root element in json. You can do it by setting:

              self.include_root_in_json  to true

              in your wrap_parameters.rb

               

              You find wrap_parameters.rbs in:

               

              your_demo_folder\config\initializers

               

              on you rails server.

               

              Here is my wrap_parameters.rbs file. It works.

               

              ===================================

              # Be sure to restart your server when you modify this file.

              # This file contains settings for ActionController::ParamsWrapper which

              # is enabled by default.

               

              # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.

              ActiveSupport.on_load(:action_controller) do

                wrap_parameters format: [:json]

              end

               

              # Disable root element in JSON by default. HERE YOU HAVE TO CHANGE FROM FALSE TO TRUE

              ActiveSupport.on_load(:active_record) do

                self.include_root_in_json = true

              end

              ===============================================

              • Re: Need help with JSON returned by the service.
                Rafal Firlejczyk

                Syrena warszawska sounds like my father's first car!!

                 

                You have two possibilities.

                1. You can adopt the output of your service to have in the json the name of the your model - here "product"

                or

                2. you can adopt the query in you source adapter of the rhoconnect server as it is written above - omit the "product"

                 

                For point 1.:

                Assuming that you are running your service on the rails server you have to enable the root element in json. You can do it by setting:

                self.include_root_in_json  to true

                in your wrap_parameters.rb

                 

                You find wrap_parameters.rbs in:

                 

                your_demo_folder\config\initializers

                 

                on you rails server.

                 

                Here is my wrap_parameters.rbs file. It works.

                 

                ===================================

                # Be sure to restart your server when you modify this file.

                # This file contains settings for ActionController::ParamsWrapper which

                # is enabled by default.

                 

                # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.

                ActiveSupport.on_load(:action_controller) do

                  wrap_parameters format: [:json]

                end

                 

                # Disable root element in JSON by default. HERE YOU HAVE TO CHANGE FROM FALSE TO TRUE

                ActiveSupport.on_load(:active_record) do

                  self.include_root_in_json = true

                end

                ===============================================