user1552900
user1552900

Reputation: 3

Extract value nested with an array of hashes in Ruby

I have an XML response from a URL that is being converted into an array of hashes which looks like:

{
  "EmployeeList"=>{
    "EmployeeProfile"=>{
      "BuildLoc"=>{"$"=>"1 Happy Place"},
      "Status"=>{"$"=>"A"},
      "SecrTitle"=>[{}, {}],
      "ID"=>{},
      "bct"=>{},
      "NUM"=>{"$"=>"1234567"},
      "BuildCity"=>{"$"=>"Dayton"},
      "BuildFloor"=>{"$"=>"6"},
      "Expense"=>{"$"=>"1345"},
      "LastName"=>{"$"=>"Smith"},
      "Middle"=>{},
      "SecrName"=>[{}, {}],
      "InternalSMTPAddress"=>{"$"=>"[email protected]"},
      "IAddress"=>{"$"=>"[email protected]"},
      "PreferredLastName"=>{},
      "DisplayName"=>{"$"=>"Joe Smith"},
      "CellPhoneNo"=>{},
      "Title"=>{"$"=>"Dr."},
      "BuildStreetAddress"=>{"$"=>"123 Happy town"},
      "BuildState"=>{"$"=>"IL"},
      "FirstName"=>{"$"=>"Joe"},
      "AltContactTitle1"=>{},
      "Dept-CostCtrNo"=>{"$"=>"129923"},
      "PreferredFirstName"=>{"$"=>"Joe"},
      "AltContactName2"=>{},
      "AltContactPhone2"=>{},
      "GDP"=>{},
      "BuildZip"=>{"$"=>"112345"},
      "RegionID"=>{"$"=>"NAMR"},
      "EmploymentType"=>{"$"=>"E"},
      "TempPhone"=>{},
      "BuildID"=>{"$"=>"01114"},
      "CountryAbbr"=>{"$"=>"USA"},
      "FaxDisp1"=>{},
      "BuildCountry"=>{"$"=>"United States"}
    }
  },
  nil=>nil
}

What's the easiest way to extract the value of "DisplayName" and "InternalSMTPAddress"?

Upvotes: 0

Views: 1107

Answers (3)

Ramesh RV
Ramesh RV

Reputation: 72

I would recommend you to use ruby gem nested_lookup.

Install the gem using command gem install nested_lookup

In your case you need the value of 'DisplayName' and 'InternalSMTPAddress'. This is what you need to do.

Rameshs-MacBook-Pro:~ rameshrv$ irb
irb(main):001:0> require 'nested_lookup'
=> true
irb(main):051:0> include NestedLookup
=> Object
# Here the test_data is the data you gave in the question
irb(main):052:0> test_data.nested_get('DisplayName')
=> {"$"=>"Joe Smith"}
irb(main):053:0> test_data.nested_get('InternalSMTPAddress')
=> {"$"=>"[email protected]"}
irb(main):054:0>

For more information please read https://github.com/rameshrvr/nested_lookup

Upvotes: 0

rogal111
rogal111

Reputation: 5933

If you need to find some key in nested hashes use this method:

def find_key(hash,key)
  hash.each {|k, v|
    return v if k==key
    tmp=find_key(v,key) if v.is_a?(Hash)
    return tmp unless tmp.nil?
  }
  return nil
end

Usage:

hash = Hash.new
hash["key1"] = "value1"
hash["key2"] = "value2"
hash["key3"] = Hash.new
hash["key3"]["key4"] = "value4"
hash["key3"]["key5"] = "value5"
hash["key6"] = Hash.new
hash["key6"]["key7"] = "value7"
hash["key6"]["key8"] = Hash.new
hash["key6"]["key8"]["key9"] = "value9"

find_key(hash,"key9") => "value9"
find_key(hash,"key8") => {"key9"=>"value9"}
find_key(hash,"dsfsdfsd") => nil

Upvotes: 0

the Tin Man
the Tin Man

Reputation: 160553

If you assign the returned hash to a variable named "hash" you can access the two desired values for those keys like:

hash['EmployeeList']['EmployeeProfile']['DisplayName']
=> {"$"=>"Joe Smith"}

and

hash['EmployeeList']['EmployeeProfile']['InternalSMTPAddress']
=> {"$"=>"[email protected]"}

If you want the actual data in them add a trailing ['$']:

hash['EmployeeList']['EmployeeProfile']['DisplayName']['$']
=> "Joe Smith"

hash['EmployeeList']['EmployeeProfile']['InternalSMTPAddress']['$']
=> "[email protected]"

Upvotes: 1

Related Questions