Ruby script for deleting some elements recursively [Q&A]

Hi
Just because I had troubles to make it work,I decided to share it maybe some other folks can use it some time.

Requirement: Delete all elements in a parsed json log where they are empty
Note: Haproxy sends empty fields as -, that's why I used eql("-") so mind changing it if you need
Note: I also delete empty arrays and hashes.

    ruby
    {
        code =>
        '

            def recursion(event,hashed,path)
                hashed.keys.each do |k|
                    child = hashed["#{k}"]
                    if (child.is_a?(String) && child.eql?("-")) || (child.is_a?(Array) && child.empty?)
                        event.remove("#{path}[#{k}]")
                    elsif child.is_a?(Hash)
                        if child.empty?
                            event.remove("#{path}[#{k}]")
                        else
                            recursion event, child, "#{path}[#{k}]"
                        end
                    end
                end
            end
            begin
                recursion event, event.to_hash, ""
            end

        '
    }

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