title: Rails app: I need to build a json object from params inside a loop
body: <p>I need to build a json object inside a loop using params. </p>
  
  <p>My params look like this...</p>
  
  <pre><code>params[:answers]
  returns =&gt; {"1"=&gt;"answer1", "2"=&gt;"answer2"}
  </code></pre>
  
  <p>The keys in this json object are the id's of the survey question.</p>
  
  <p>So I planed to loop through the keys to build the json object like this...</p>
  
  <pre><code>def build_answersheet_json(params[:answers], params[:survey_id])
    params[:answers].keys.each do |question_id|
      current_question = question_id
      current_answer = params[:answers][question_id]
    end
  end
  </code></pre>
  
  <p>Since im using "t.json" in my migration to save json to postgres, I wanted to use the extracted question_id and answer to build a json object that looks something like this...</p>
  
  <pre><code>{
    survey_id: '1',
    answers: {
      question: [{
        question_id: 1,
        answer: 'answer1'
      }, {
        question_id: 2,
        answer: 'answer2'
        }]
    }
  }
  </code></pre>
  
  <p>Ive been trying to do this using a method that looks somthing like this...</p>
  
  <pre><code>build_answersheet_json(params[:answers], params[:survey_id])
  </code></pre>
  
  <p>Ive tried JSON.parse() and Ive tried to just logically work through it but I cant seem to figure this out. </p>
  
  <p>Any help is appreciated.</p>