How to remove “id” from json string in ruby on rails?












1















I have got data without "id" field from database. But when I parse the data to json string and show it on screen, I saw an "id" field on json string. How to remove this "id" field?



Here is my code:



-report_logic.rb



def fetch_data ()
@datalist=WorkEarlyOverTime
@datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
@datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`')
@datalist = @datalist.where(work_id: $work_id)
return @datalist
end


- report_controller.rb



def work_late
report_logic = ReportLogic.new(params)
@work_late = report_logic.fetch_data()
render action: :work_late
end


- work_late.json.jbuilder



json.一覧 @work_late


When I show the string I expected the output is:



{"一覧":[
{"1次協力会社名":1,
"職種名":"0",
"作業場所":"0.00 ",
"作業内容":"2000-01-01T19:00:00.000+00:00",
"contents":"作業内容1"}
]}


but the actual output is:



{"一覧":[
{"id":null,
"1次協力会社名":1,
"職種名":"0",
"作業場所":"0.00 ",
"作業内容":"2000-01-01T19:00:00.000+00:00",
"contents":"作業内容1"}
]}









share|improve this question



























    1















    I have got data without "id" field from database. But when I parse the data to json string and show it on screen, I saw an "id" field on json string. How to remove this "id" field?



    Here is my code:



    -report_logic.rb



    def fetch_data ()
    @datalist=WorkEarlyOverTime
    @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
    @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`')
    @datalist = @datalist.where(work_id: $work_id)
    return @datalist
    end


    - report_controller.rb



    def work_late
    report_logic = ReportLogic.new(params)
    @work_late = report_logic.fetch_data()
    render action: :work_late
    end


    - work_late.json.jbuilder



    json.一覧 @work_late


    When I show the string I expected the output is:



    {"一覧":[
    {"1次協力会社名":1,
    "職種名":"0",
    "作業場所":"0.00 ",
    "作業内容":"2000-01-01T19:00:00.000+00:00",
    "contents":"作業内容1"}
    ]}


    but the actual output is:



    {"一覧":[
    {"id":null,
    "1次協力会社名":1,
    "職種名":"0",
    "作業場所":"0.00 ",
    "作業内容":"2000-01-01T19:00:00.000+00:00",
    "contents":"作業内容1"}
    ]}









    share|improve this question

























      1












      1








      1








      I have got data without "id" field from database. But when I parse the data to json string and show it on screen, I saw an "id" field on json string. How to remove this "id" field?



      Here is my code:



      -report_logic.rb



      def fetch_data ()
      @datalist=WorkEarlyOverTime
      @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
      @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`')
      @datalist = @datalist.where(work_id: $work_id)
      return @datalist
      end


      - report_controller.rb



      def work_late
      report_logic = ReportLogic.new(params)
      @work_late = report_logic.fetch_data()
      render action: :work_late
      end


      - work_late.json.jbuilder



      json.一覧 @work_late


      When I show the string I expected the output is:



      {"一覧":[
      {"1次協力会社名":1,
      "職種名":"0",
      "作業場所":"0.00 ",
      "作業内容":"2000-01-01T19:00:00.000+00:00",
      "contents":"作業内容1"}
      ]}


      but the actual output is:



      {"一覧":[
      {"id":null,
      "1次協力会社名":1,
      "職種名":"0",
      "作業場所":"0.00 ",
      "作業内容":"2000-01-01T19:00:00.000+00:00",
      "contents":"作業内容1"}
      ]}









      share|improve this question














      I have got data without "id" field from database. But when I parse the data to json string and show it on screen, I saw an "id" field on json string. How to remove this "id" field?



      Here is my code:



      -report_logic.rb



      def fetch_data ()
      @datalist=WorkEarlyOverTime
      @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
      @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`')
      @datalist = @datalist.where(work_id: $work_id)
      return @datalist
      end


      - report_controller.rb



      def work_late
      report_logic = ReportLogic.new(params)
      @work_late = report_logic.fetch_data()
      render action: :work_late
      end


      - work_late.json.jbuilder



      json.一覧 @work_late


      When I show the string I expected the output is:



      {"一覧":[
      {"1次協力会社名":1,
      "職種名":"0",
      "作業場所":"0.00 ",
      "作業内容":"2000-01-01T19:00:00.000+00:00",
      "contents":"作業内容1"}
      ]}


      but the actual output is:



      {"一覧":[
      {"id":null,
      "1次協力会社名":1,
      "職種名":"0",
      "作業場所":"0.00 ",
      "作業内容":"2000-01-01T19:00:00.000+00:00",
      "contents":"作業内容1"}
      ]}






      sql ruby-on-rails json






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 7:01









      SpiritSpirit

      113




      113
























          2 Answers
          2






          active

          oldest

          votes


















          0














          As your desired output is a JSON, you could try .as_json by excepting the id field as follows:



          @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)


          Your detailed fetch_data function will be like this:



          def fetch_data ()
          @datalist=WorkEarlyOverTime
          @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
          @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
          @datalist = @datalist.where(work_id: $work_id)
          return @datalist
          end





          share|improve this answer
























          • thanks, your solution is very helpful :D

            – Spirit
            Jan 2 at 9:44



















          0














          If id is always null then just ignore all nils:



          json.ignore_nil!
          json.一覧 @work_late


          If that's not the case then you have to filter out the id before serializing it. One way would be:



          json.一覧 @work_late.serializable_hash.except("id")





          share|improve this answer
























          • thanks, but it didn't work in my code :(

            – Spirit
            Jan 2 at 9:42











          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%2f54002459%2fhow-to-remove-id-from-json-string-in-ruby-on-rails%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









          0














          As your desired output is a JSON, you could try .as_json by excepting the id field as follows:



          @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)


          Your detailed fetch_data function will be like this:



          def fetch_data ()
          @datalist=WorkEarlyOverTime
          @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
          @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
          @datalist = @datalist.where(work_id: $work_id)
          return @datalist
          end





          share|improve this answer
























          • thanks, your solution is very helpful :D

            – Spirit
            Jan 2 at 9:44
















          0














          As your desired output is a JSON, you could try .as_json by excepting the id field as follows:



          @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)


          Your detailed fetch_data function will be like this:



          def fetch_data ()
          @datalist=WorkEarlyOverTime
          @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
          @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
          @datalist = @datalist.where(work_id: $work_id)
          return @datalist
          end





          share|improve this answer
























          • thanks, your solution is very helpful :D

            – Spirit
            Jan 2 at 9:44














          0












          0








          0







          As your desired output is a JSON, you could try .as_json by excepting the id field as follows:



          @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)


          Your detailed fetch_data function will be like this:



          def fetch_data ()
          @datalist=WorkEarlyOverTime
          @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
          @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
          @datalist = @datalist.where(work_id: $work_id)
          return @datalist
          end





          share|improve this answer













          As your desired output is a JSON, you could try .as_json by excepting the id field as follows:



          @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)


          Your detailed fetch_data function will be like this:



          def fetch_data ()
          @datalist=WorkEarlyOverTime
          @datalist = @datalist.joins("INNER JOIN `works` ON `work_early_over_times`.`work_id` = `works`.`id`")
          @datalist = @datalist.select('`work_early_over_times`.work_id AS 1次協力会社名','`work_early_over_times`.working_temp_person AS 職種名','`work_early_over_times`.working_temp_hour AS 作業場所','`work_early_over_times`.at_time_overtime_start AS 作業内容','`works`.`contents`').as_json(:except => :id)
          @datalist = @datalist.where(work_id: $work_id)
          return @datalist
          end






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 9:07









          SandySandy

          1,6922722




          1,6922722













          • thanks, your solution is very helpful :D

            – Spirit
            Jan 2 at 9:44



















          • thanks, your solution is very helpful :D

            – Spirit
            Jan 2 at 9:44

















          thanks, your solution is very helpful :D

          – Spirit
          Jan 2 at 9:44





          thanks, your solution is very helpful :D

          – Spirit
          Jan 2 at 9:44













          0














          If id is always null then just ignore all nils:



          json.ignore_nil!
          json.一覧 @work_late


          If that's not the case then you have to filter out the id before serializing it. One way would be:



          json.一覧 @work_late.serializable_hash.except("id")





          share|improve this answer
























          • thanks, but it didn't work in my code :(

            – Spirit
            Jan 2 at 9:42
















          0














          If id is always null then just ignore all nils:



          json.ignore_nil!
          json.一覧 @work_late


          If that's not the case then you have to filter out the id before serializing it. One way would be:



          json.一覧 @work_late.serializable_hash.except("id")





          share|improve this answer
























          • thanks, but it didn't work in my code :(

            – Spirit
            Jan 2 at 9:42














          0












          0








          0







          If id is always null then just ignore all nils:



          json.ignore_nil!
          json.一覧 @work_late


          If that's not the case then you have to filter out the id before serializing it. One way would be:



          json.一覧 @work_late.serializable_hash.except("id")





          share|improve this answer













          If id is always null then just ignore all nils:



          json.ignore_nil!
          json.一覧 @work_late


          If that's not the case then you have to filter out the id before serializing it. One way would be:



          json.一覧 @work_late.serializable_hash.except("id")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 7:40









          sloneorzeszkisloneorzeszki

          164111




          164111













          • thanks, but it didn't work in my code :(

            – Spirit
            Jan 2 at 9:42



















          • thanks, but it didn't work in my code :(

            – Spirit
            Jan 2 at 9:42

















          thanks, but it didn't work in my code :(

          – Spirit
          Jan 2 at 9:42





          thanks, but it didn't work in my code :(

          – Spirit
          Jan 2 at 9:42


















          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%2f54002459%2fhow-to-remove-id-from-json-string-in-ruby-on-rails%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

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory