I'm trying to extract the values inside a nested hash with values inside an array within a hash value (basically a nested hash-array value) into individual string values. Hash value sample is below:
{"Video Analysis"=>{
"Video Width"=>["1920"],
"Video Height"=>["1080"],
"Video Framerate"=>["29.97"],
"Video Bitrate"=>["50000000"],
"Interlaced Video"=>["True"],
"Header Duration"=>["00:04:59:[email protected]"],
"Content Duration"=>["00:01:59:[email protected]"],
"Relative Gated Loudness"=>["-23.115095138549805"],
"True Peak Signal"=>["-5.3511543273925781"]}}
And the expected output shall be individual string values like this:
Video Width = 1920
Video Height = 1080...
I actually made a code but it gets larger as I extract each hash-array value individually
labels_hash = inputs['labels_hash'].select{ |k,v| k == 'Video Analysis'}.values.flatten
labels_subhash = vantage_labels_hash[0]
OTTVideoWidthArray = labels_subhash.select{ |k,v| k == 'Video Width'}.values.flatten
outputs['Video Width'] = OTTVideoWidthArray[0].to_f
OTTVideoHeightArray = labels_subhash.select{ |k,v| k == 'Video Height'}.values.flatten
outputs['Video Height'] = OTTVideoHeightArray[0].to_f
So I wanted to have something that is shorter to run. Hope you can help. Thanks!
his a variable that holds your hash I suggest simplyh["Video Analysis"].transform_values(&:first) #=> {"Video Width"=>"1920", "Video Height"=>"1080",...,"True Peak Signal"=>"-5.3511543273925781"}. See Hash#transform_values.