This filter generates two fields with an array of values:
xml.results.Name: ["OrderId","CaseId","Timestamp"]
xml.results.Value: ["123456","987654","1629106126536"]
The goal is to create new fields based on the Name and Value:
xml.results.Name.OrderId: "123456"
xml.results.Name.CaseId: "987654"
...
As there are hundreds of different field names the "xpath" definition is not good solution for us.
Is there an option in logstash how to do it?
That is telling you that k is nil, which means you are processing an event where the [xml][results][Name] field does not exist. I would wrap the k.each_index with
if k.method_defined?(each_index) and v.method_defined?(each_index) {
...
}
ruby {
code => '
v = event.get("[xml][results][Value]")
k = event.get("[xml][results][Name]")
if k.method_defined?(each_index) and v.method_defined?(each_index) {
k.each_index { |i|
event.set("[xml][results][Name][" + k[i] + "]", v[i] )
}
}
'
}
But following error appeared:
[2021-08-17T11:29:13,485][FATAL][org.logstash.Logstash ] Logstash stopped processing because of an error: (SyntaxError) (ruby filter code):11: syntax error, unexpected end-of-file
org.jruby.exceptions.SyntaxError: (SyntaxError) (ruby filter code):11: syntax error, unexpected end-of-file
at org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1048) ~[jruby-complete-9.2.13.0.jar:?]
The second question is why the field does not exist? We can see the field name and an array of values in the event json (as described in the 1st post).
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.