The workaround is to switch the LHS and RHS around.
Reasoning:
when v is the Timestamp the v == '-' is calling the ==(other) method on the Timestamp instance and this does a naive other.time without checking whether other (the String '-') is the same Class as v.
By switching them around the `==(other) call no happens on the String class and it in not so naive - it does better equality checks.
Working code:
input {
generator {
message => '{"fld_1": "a`b ", "fld_2": "-", "fld_3": 7}'
count => 2
}
}
filter {
json {
source => "message"
}
ruby {
code => "
hash = event.to_hash
hash.each do |k,v|
if '-' == v
event.remove(k)
else
if v.is_a? String
v.strip!
v.gsub!('`', ',')
event.set(k, v)
end
end
end
"
}
}
output {
stdout { codec => rubydebug }
}
NOTE: if you intend that the v.gsub!('', ',')actually modifies the event then you must set it back into the event, becausev` is not guaranteed to always be a reference to the value in the event.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.