qbantek
qbantek

Reputation: 695

What would be the best way to convert this hash?

I have a Ruby hash that I need to convert to another format. Considering that the 'array' size is unknown/unlimited, how would you 'flatten' this hash into the desired format?

Original Hash

{
  :parameters => {
    :'userResponse.objectInstanceType' => 'QuesAnsResponse',
    :'userResponse.quesAnsDetailArray' => {
      :'0' => {
        '.answer'=> 'Texas'
      },
      :'1' => {
        '.answer' => 'w3schools'
      }
    }
  }
}

Desired Format:

{
  :parameters => {
    :'userResponse.objectInstanceType' => 'QuestionAnsResponse',
    :'userResponse.quesAnsDetailArray[0].answer' => 'Texas',    
    :'userResponse.quesAnsDetailArray[1].answer' => 'w3schools'
  }
}

Upvotes: 0

Views: 51

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110645

I have assumed the hash is as follows:

h = {
  'parameters'=> {
    'userResponse.objectInstanceType'=> 'QuesAnsResponse',
    'userResponse.quesAnsDetailArray'=> {
      '0'=> {
        '.answer'=> '',
        '.answerFieldType'=> 'text',
        '.isRequired'=> 'true',
        '.metaData'=> 'QUESTION_1',
        '.questionFieldType'=> 'label',
        '.question'=> 'What is the name of your state?'
      },
      '1'=> {
        '.answer'=> '',
        '.answerFieldType'=> 'text',
        '.isRequired'=> 'true',
        '.metaData'=> 'QUESTION_2',
        '.questionFieldType'=> 'label',
        '.question'=> 'What is the name of your first school'
      }
    }
  }
}

and would convert it to the desired format as follows:

{ 'parameters'=> Hash[[
    ['userResponse.objectInstanceType',
      h['parameters']['userResponse.objectInstanceType']],
     *h['parameters']['userResponse.quesAnsDetailArray'].
       flat_map { |ndx,f|
         ["userResponse.quesAnsDetailArray[#{ndx}]"].product(f.to_a) }.
           map { |prefix,(suffix,value)| [prefix+suffix, value] } ]]      
}
  #=> {"parameters"=>
    {"userResponse.objectInstanceType"=>"QuesAnsResponse", 
     "userResponse.quesAnsDetailArray[0].answer"=>"", 
     "userResponse.quesAnsDetailArray[0].answerFieldType"=>"text", 
     "userResponse.quesAnsDetailArray[0].isRequired"=>"true", 
     "userResponse.quesAnsDetailArray[0].metaData"=>"QUESTION_1", 
     "userResponse.quesAnsDetailArray[0].questionFieldType"=>"label", 
     "userResponse.quesAnsDetailArray[0].question"=>
       "What is the name of your state?",
     "userResponse.quesAnsDetailArray[1].answer"=>"", 
     "userResponse.quesAnsDetailArray[1].answerFieldType"=>"text", 
     "userResponse.quesAnsDetailArray[1].isRequired"=>"true", 
     "userResponse.quesAnsDetailArray[1].metaData"=>"QUESTION_2", 
     "userResponse.quesAnsDetailArray[1].questionFieldType"=>"label", 
     "userResponse.quesAnsDetailArray[1].question"=>
       "What is the name of your first school"}} 

Upvotes: 1

mikej
mikej

Reputation: 66263

If you can make a few assumptions about the structure of the input i.e. that it has the numbered responses inside a "userResponse.quesAnsDetailArray" section then you could do something along these lines:

new_hash = { 'parameters' => 
  { 'userResponse.objectInstanceType' => 'QuestionAnsResponse' } }

hash['parameters']['userResponse.quesAnsDetailArray'].each_pair do |index, details|
  details.each_pair do |field, value|
    new_hash['parameters']["userResponse.quesAnsDetailArray[#{index}]#{field}"] = value
  end
end

Upvotes: 2

Related Questions