Reputation: 348
I have the following Hash with a array inside a array inside the hash. The inner array is a array of hashes. I am having trouble figuring out how to access them.
{"AssetWarrantyResponse"=>
[{"AssetHeaderData"=>
{"BUID"=>"11", "ServiceTag"=>"GH3RV12", "ShipDate"=>"2014-06-05T00:00:00", "CountryLookupCode"=>"US", "LocalChannel"=>"45", "CustomerNumber"=>"127963428", "ItemClassCode"=>"RK003", "IsDuplicate"=>false, "MachineDescription"=>"OptiPlex 3020", "OrderNumber"=>"631899941", "ParentServiceTag"=>nil},
"ProductHeaderData"=>
{"SystemDescription"=>"OptiPlex 3020", "ProductId"=>"optiplex-3020-desktop", "ProductFamily"=>"Desktops & All-in-Ones", "LOB"=>"OptiPlex", "LOBFriendlyName"=>"OptiPlex"},
"AssetEntitlementData"=>[
{"StartDate"=>"2022-06-07T00:00:00", "EndDate"=>"2022-06-08T00:00:00", "ServiceLevelDescription"=>"Dell", "ServiceLevelCode"=>"DL", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"999-0036"},
{"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-05T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0052"},
{"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-7368"},
{"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7738"},
{"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-6888"},
{"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7718"},
{"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0008"},
{"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9491"},
{"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9982"}
]}
],
"InvalidFormatAssets"=>{"BadAssets"=>[]}, "InvalidBILAssets"=>{"BadAssets"=>[]}, "ExcessTags"=>{"BadAssets"=>[]}, "AdditionalInformation"=>nil}
The following code returns the full array of hashed instead of looping through the hashes.
@hash['AssetWarrantyResponse'][0]['AssetEntitlementData'].each do |k| k['StartDate'] end
returns:
[{"StartDate"=>"2022-06-07T00:00:00", "EndDate"=>"2022-06-08T00:00:00", "ServiceLevelDescription"=>"Dell", "ServiceLevelCode"=>"DL", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"999-0036"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-05T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0052"}, {"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-7368"}, {"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7738"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-6888"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7718"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0008"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9491"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9982"}]
Could someone point me in the right direction? I want to loop through each hash.
Upvotes: 0
Views: 43
Reputation: 727
Array#each returns the original array. In calling each
with a block on the "AssetEntitlementData"
, you'll call the block on each hash in the array, and then return the original array. If you're trying to return an array of "StartDate"
values, try this:
@hash['AssetWarrantyResponse'][0]['AssetEntitlementData'].map do |h|
h['StartDate']
end
Array#map will return an array with the result of calling the block on each element.
Upvotes: 1