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

**URL:** https://discuss.elastic.co/t/need-to-improve-my-ruby-code-to-convert-the-data-in-right-format/287801
**Category:** Logstash
**Created:** [October 27, 2021, 1:08pm UTC](https://discuss.elastic.co/t/need-to-improve-my-ruby-code-to-convert-the-data-in-right-format/287801 "2021-10-27T13:08:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)
#### Post date: [October 27, 2021, 1:08pm UTC](https://discuss.elastic.co/t/need-to-improve-my-ruby-code-to-convert-the-data-in-right-format/287801/1 "2021-10-27T13:08:25Z")

</div>

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

I am getting three arrays. Something on these lines:

```auto
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:

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

```

Code:

```auto
ruby
{
		code => "idarray = event.get('[ArrayOfId]')
				 valuearray = event.get('[ArrayOfValue]')
				 idarray.zip(valuearray).each { |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?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [October 27, 2021, 3:56pm UTC](https://discuss.elastic.co/t/need-to-improve-my-ruby-code-to-convert-the-data-in-right-format/287801/2 "2021-10-27T15:56:03Z")

</div>

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 = LogStash::Timestamp.new(date.to_time)
                else
                    newV = values[x]
                end
                event.set(ids[x], newV)
            }
        '
    }

```

which will produce

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

---

<div class="post-metadata">

### Author: ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)
#### Post date: [October 28, 2021, 4:08am UTC](https://discuss.elastic.co/t/need-to-improve-my-ruby-code-to-convert-the-data-in-right-format/287801/3 "2021-10-28T04:08:19Z")

</div>

Works like a charm. Thanks Badger !!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 25, 2021, 4:08am UTC](https://discuss.elastic.co/t/need-to-improve-my-ruby-code-to-convert-the-data-in-right-format/287801/4 "2021-11-25T04:08:51Z")

</div>

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