ruby - Storing duplicate keys in hash -


i expecting duplicate key stored value in student_hash, due absence of multimap in ruby, not allowed. 1 solution capture information in separate 2 arrays based on selection(id/type/school_id), input data might have depth, , array size cannot determined. can same behavior (expected output) achieved using hash? please suggest.

expected output:

{:id=>"101", :type=>"junior", :school_id=>"is1.4598"} {:id=>"103", :type=>"senior", :school_id=>"is1.098"} 

code:

require 'json'  raw_data='{   "enquiry": "get_all_student_info",   "success": true,   "payload": [     {       "standard_version": "1.4",       "country_id": "usa_01",       "parent": "usa_is1",       "id": "101",       "company": "govt",       "type": "junior",       "subsystem_type": "govtgirlsschool",       "school_id": "is1.098"      },     {       "standard_version": "1.4",       "country_id": "nz_01",       "parent": "nz_is1",       "id": "103",       "company": "private",       "type": "senior",       "subsystem_type": "govtboysschool",       "school_id": "is1.098"      }   ],   "error": "" }'  def student_hash(set, result = {})   if set.class == hash      set.each |k, v|       if("#{k}"=="id" || "#{k}"=="type"  || "#{k}"=="school_id")         puts result ["#{k}".to_sym] = "#{v}"       end       if v.class == hash         result = student_hash(v, result)       elsif v.class == hash || v.class == array         result = student_hash(v, result)        end     end   elsif set.class == array     set.each |a|       result = student_hash(a, result)     end   end   result end  student_hash(json.parse(raw_data)) # => {:id=>"103", :type=>"senior", :school_id=>"is1.098"} 

not clear but, enough expected output raw_data

data = json.parse(raw_data) data["payload"].map {|i| { :id => i["id"], type: i["type"], school_id: i["school_id"] }} 

now can print hoe ever want.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -