how to return selected data on json api












1















i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?



    my controller  
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",

},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]









share|improve this question

























  • use select option

    – Nilay Singh
    Nov 13 '18 at 7:47











  • Go through gem act_as_api. It is really very good gem to create json response for API.

    – ray
    Nov 13 '18 at 8:25
















1















i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?



    my controller  
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",

},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]









share|improve this question

























  • use select option

    – Nilay Singh
    Nov 13 '18 at 7:47











  • Go through gem act_as_api. It is really very good gem to create json response for API.

    – ray
    Nov 13 '18 at 8:25














1












1








1








i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?



    my controller  
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",

},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]









share|improve this question
















i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?



    my controller  
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",

},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]






ruby-on-rails json ruby api ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:00







giri dharan

















asked Nov 13 '18 at 7:45









giri dharangiri dharan

2939




2939













  • use select option

    – Nilay Singh
    Nov 13 '18 at 7:47











  • Go through gem act_as_api. It is really very good gem to create json response for API.

    – ray
    Nov 13 '18 at 8:25



















  • use select option

    – Nilay Singh
    Nov 13 '18 at 7:47











  • Go through gem act_as_api. It is really very good gem to create json response for API.

    – ray
    Nov 13 '18 at 8:25

















use select option

– Nilay Singh
Nov 13 '18 at 7:47





use select option

– Nilay Singh
Nov 13 '18 at 7:47













Go through gem act_as_api. It is really very good gem to create json response for API.

– ray
Nov 13 '18 at 8:25





Go through gem act_as_api. It is really very good gem to create json response for API.

– ray
Nov 13 '18 at 8:25












2 Answers
2






active

oldest

votes


















1














You will have to understand about JSON builders,



start here https://www.ruby-toolbox.com/categories/API_Builders



Choose one and get along .



I have used Jbuilder quite a few times






share|improve this answer































    1














    create json-jbuilder view




    app/[controller]/show.json.jbuilder




    json.id address.id
    json.user_id address.user_id
    json.city address.city
    ...



    your controller action




       def show
    @address = Address.where(user_id:params[:id])
    end


    for more details you may read jBuilder documentation https://github.com/rails/jbuilder






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53276132%2fhow-to-return-selected-data-on-json-api%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














      You will have to understand about JSON builders,



      start here https://www.ruby-toolbox.com/categories/API_Builders



      Choose one and get along .



      I have used Jbuilder quite a few times






      share|improve this answer




























        1














        You will have to understand about JSON builders,



        start here https://www.ruby-toolbox.com/categories/API_Builders



        Choose one and get along .



        I have used Jbuilder quite a few times






        share|improve this answer


























          1












          1








          1







          You will have to understand about JSON builders,



          start here https://www.ruby-toolbox.com/categories/API_Builders



          Choose one and get along .



          I have used Jbuilder quite a few times






          share|improve this answer













          You will have to understand about JSON builders,



          start here https://www.ruby-toolbox.com/categories/API_Builders



          Choose one and get along .



          I have used Jbuilder quite a few times







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 7:54









          NithinNithin

          2,96231748




          2,96231748

























              1














              create json-jbuilder view




              app/[controller]/show.json.jbuilder




              json.id address.id
              json.user_id address.user_id
              json.city address.city
              ...



              your controller action




                 def show
              @address = Address.where(user_id:params[:id])
              end


              for more details you may read jBuilder documentation https://github.com/rails/jbuilder






              share|improve this answer




























                1














                create json-jbuilder view




                app/[controller]/show.json.jbuilder




                json.id address.id
                json.user_id address.user_id
                json.city address.city
                ...



                your controller action




                   def show
                @address = Address.where(user_id:params[:id])
                end


                for more details you may read jBuilder documentation https://github.com/rails/jbuilder






                share|improve this answer


























                  1












                  1








                  1







                  create json-jbuilder view




                  app/[controller]/show.json.jbuilder




                  json.id address.id
                  json.user_id address.user_id
                  json.city address.city
                  ...



                  your controller action




                     def show
                  @address = Address.where(user_id:params[:id])
                  end


                  for more details you may read jBuilder documentation https://github.com/rails/jbuilder






                  share|improve this answer













                  create json-jbuilder view




                  app/[controller]/show.json.jbuilder




                  json.id address.id
                  json.user_id address.user_id
                  json.city address.city
                  ...



                  your controller action




                     def show
                  @address = Address.where(user_id:params[:id])
                  end


                  for more details you may read jBuilder documentation https://github.com/rails/jbuilder







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 7:54









                  Sikandar TariqSikandar Tariq

                  415414




                  415414






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f53276132%2fhow-to-return-selected-data-on-json-api%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

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma