0

I need to build a json object inside a loop using params.

My params look like this...

params[:answers]
returns => {"1"=>"answer1", "2"=>"answer2"}

The keys in this json object are the id's of the survey question.

So I planed to loop through the keys to build the json object like this...

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

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...

{
  survey_id: '1',
  answers: {
    question: [{
      question_id: 1,
      answer: 'answer1'
    }, {
      question_id: 2,
      answer: 'answer2'
      }]
  }
}

Ive been trying to do this using a method that looks somthing like this...

build_answersheet_json(params[:answers], params[:survey_id])

Ive tried JSON.parse() and Ive tried to just logically work through it but I cant seem to figure this out.

Any help is appreciated.

3 Answers 3

1

Maybe you can try something like that:

/* fake params (to test) */
params = {
    survey_id: '1',
    answers: {
        "1"=>"answer1", 
        "2"=>"answer2", 
        "3"=>"answer3", 
        "4"=>"answer4"
    }
}

def build_answersheet_json(answers, survey_id)
  {
    survey_id: survey_id,
    answers: answers.map { |k,v| { question_id: k.to_i, answer: v } }
  }
end


survey = build_answersheet_json(params[:answers], params[:survey_id])

puts survey.class 
#Hash

puts survey.to_json
# formated JSON string:
# {
#   "survey_id":"1",
#   "answers":[
#       {"question_id":1,"answer":"answer1"},
#       {"question_id":2,"answer":"answer2"},
#       {"question_id":3,"answer":"answer3"},
#       {"question_id":4,"answer":"answer4"}
#   ]
# }

In order to save to a t.json postgress column type, just pass the Hash survey object, like that:

YourModel.create(survey: survey)

Source: http://edgeguides.rubyonrails.org/active_record_postgresql.html

Sign up to request clarification or add additional context in comments.

2 Comments

This looks uber promising. Thanks for the quick reply. I have to hit the sack right now but I will run this through in the morning.
Thanks! This worked perfectly. Thanks for citing the article too!
1

Try

{
  survey: ¯\_༼◉ل͟◉༽_/¯,
}

Comments

0

Json may not be parsed if json have construction like this:

survey = {
}

Json may not contain = and assignment

Check real variables values with puts varname.inspect near at code lines where you meet unexpected behaviour.

1 Comment

Im sorry, the survey = {} should not be there. I just want the final object to look like what is after the " = " I will edit the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.