Need to improve my ruby code to convert the data in right format

I am using XPATH in XML filter to extract some values.

I am getting three arrays. Something on these lines:

ArrayOfId = ["SamplingRate","Humidity","DateOfCalibration","ProductFamily"]
ArrayOfType = ["Number","Number","Date","Text"]
ArrayOfValue = ["23","21","27/08/2021","Hunira"]

I am using Ruby to combine these into a single event. I am able to get something like this:

"SamplingRate" => "23"
"Humidity" => "21"
"DateOfCalibration" => "27/08/2021"
"ProductFamily" => "Hunira"


		code => "idarray = event.get('[ArrayOfId]')
				 valuearray = event.get('[ArrayOfValue]') { |key, value| event.set(key, value); }"

I am stuck at how to use the information in the ArrayOfType to make proper conversions in logstash. For example I will like to map every Number as float. I am ok to do this in Ruby filter level.

Any ideas?

You could start with

    ruby {
        code => '
            ids = event.get("ArrayOfId")
            types = event.get("ArrayOfType")
            values = event.get("ArrayOfValue")
            ids.each_index { |x|
                case types[x]
                when "Text"
                    newV = values[x].to_s
                when "Number"
                    newV = values[x].to_f
                when "Date"
                    date = Date.strptime(values[x], "%d/%m/%Y")
                    newV =
                    newV = values[x]
                event.set(ids[x], newV)

which will produce

         "Humidity" => 21.0,
     "SamplingRate" => 23.0,
    "ProductFamily" => "Hunira",
"DateOfCalibration" => 2021-08-27T04:00:00.000Z,

Works like a charm. Thanks Badger !!

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