Recursively removing `nil` and empty values from hash

ruby remove empty string values from hash
remove empty from hash
ruby hash remove nil values
rails remove empty string from hash
remove keys with nil values ruby
remove empty from hash ruby
ruby array remove empty strings
ruby hash select not nil

I have a hash that starts out as:

{"request"=>{
  "security"=>{"username"=>{"type"=>nil}, "password"=>{"type"=>nil}},
  "order_i_d"=>{"type"=>nil, "description"=>nil},
  "order_number"=>{"type"=>nil},
  "show_kit_as_individual_s_k_us"=>false,
  "website_i_d"=>{"type"=>nil, "description"=>nil}
}}

And I'd like to recursively remove all values that are nil? and empty? but leave the falsey values in place. The final result should look like:

{"request"=>{
  "show_kit_as_individual_s_k_us"=>false
}}

How can I accomplish this?

Removing all empty elements from a hash / YAML?, You could add a compact method to Hash like this class Hash def compact delete_if { |k, v| v.nil? } end end. or for a version that supports recursion class Hash� I believe it would be best to use a self recursive method. That way it goes as deep as is needed. This will delete the key value pair if the value is nil or an empty Hash. class Hash def compact delete_if {|k,v| v.is_a?(Hash) ? v.compact.empty? : v.nil? } end end Then using it will look like this:

Came up with the following:

class Hash  
  def deep_compact!
    self.each_pair do |key, value|
      if value.is_a?(Hash)
        value.deep_compact!
      end
      if value.nil? || (value.is_a?(Hash) && value.empty?)
        self.delete(key)
      end
    end
  end
end

Delete all empty/false elements from hash recursively � GitHub, Delete all empty/false elements from hash recursively. gistfile1.rb. # (v. respond_to?(:empty?) ? v.empty? : !v) is basically rails' .blank? in plain� We first recursively walk through the whole data structure that the API converts to a Ruby hash for us. We get all nested field names and store in an array their Logstash-style paths like "[nginx][access][upstream_addr]" that the API expects. Then we walk through the paths and use the API to check for empty values, and remove them.

Another option:

class Hash
    def deep_transform(&block)
      self.inject({}){|result, (key,value)|
        value = if Hash === value
                  value.deep_transform(&block)
                else
                  value
                end
        block.call(result,key,value)
        result
      }
    end

    def deep_compact
      self.deep_transform do |result, key, value|
        if value.nil? || (value.is_a?(Hash) && value.empty?)
          # Don't Keep
        else
          result[key] = value
        end
      end
    end
end

Recursively remove nil values and empty string/array/hash values , Recursively remove nil values and empty string/array/hash values.(Modified from juwalter's answer on� More advanced Perl hash tutorials: How to see if a Perl hash contains a key; How to delete an element from a Perl hash by key; The hash in Perl - hash sorting tutorials: Perl hash sorting - Sort a Perl hash by the hash key; Perl hash sorting - Sort a Perl hash by the hash value

Class: Hash (Ruby 2.4.2), Returns a new, empty hash. If this hash is subsequently accessed Returns a new hash with the nil values/key pairs removed h = { a: 1, b: false, c: nil } Unlike Array#flatten, this method does not flatten recursively by default. The optional level� Recursive aliases were introduced when we were implementing JSON parsing. We needed a type that could be Nil, Bool, Int64, Float64, String or an array of those same things, recursively, or a hash with values of those same things, recursively. So we decided that having a type that could reference itself in its definition was the way to go.

Ruby: Removing all empty elements from a hash / YAML? Quick , Rails 4.1 added Hash#compact and Hash#compact! as a core hash = { a: true, b: false, c: nil} hash.compact # => { a: true, b: false} hash� Filtering out nil values in Logstash. Andrii Abramov. Follow. Feb 17, Unfortunately, Logstash has not built-in to remove nil values from events. Solution:

Program to remove empty array elements in PHP, Given an array containing elements. The task is to remove empty elements from the array such as an empty string or a NULL element. Method 1: Using� Given various definitions of "empty", I will demonstrate how to remove / filter out empty elements from an array in PowerShell. If you have integers with the

Comments
  • So you already know you have to use recursion. Combine it with Hash#delete and you're good to go.
  • Are you confusing the term falsey? falsey means nil or false. If you mean just false, then why not write so?
  • Original deep_transform method grabbed from virtuouscode.com/2009/11/20/hash-transforms-in-ruby
  • It's usually best to add both options to the same answer than to have two contradictory answers.