How to run a ruby function that updates and event and clears the empty fields recursively

I got an event which sometimes contain empty fields. I would like to delete those fields which are null and those which are empty.

Check this code, works pretty good.

This avoids needing to use LogStash::Event stuff which overcomplicates things. Then you just need to pass the event to the recursive function.

          def delete_unknowns_from_hash(event, parents, new_hash)
            new_hash.each { |k, v|                  
              if v.is_a? String and ( v == "unknown" || v =="" )  then
                event.remove(parents + "[#{k}]")
              elsif v == nil then
                event.remove(parents + "[#{k}]")
              elsif v.is_a? Hash then
                delete_unknowns_from_hash(event, parents + "[#{k}]", v)
              end
            }
          end

          delete_unknowns_from_hash(event,"",event.to_hash)

Beautiful! Thanks! :smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.