# Convert values in kv-pairs (using ruby filter)

**URL:** <https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035>\
**Category:** Logstash\
**Created:** [June 7, 2018, 5:29pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035 "2018-06-07T17:29:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oleg31337](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@oleg31337](https://discuss.elastic.co/u/oleg31337)\
**Post date:** [June 7, 2018, 5:29pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035/1 "2018-06-07T17:29:24Z")

</div>

Hi there!  
There was a topic already for that: [https://discuss.elastic.co/t/convert-values-in-kv-pairs/39904](https://discuss.elastic.co/t/convert-values-in-kv-pairs/39904)  
but the solution no longer works due to syntax changes in new version.  
Could anyone please advise what it should look like now?  
old syntax looks like this:

ruby{ code =\> " kv = event['kvpairs'] kv.to\_hash.keys.each { |k| if kv[k].include? '.' kv[k] = kv[k].to\_f else kv[k] = kv[k].to\_i end } " }

---

<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:** [June 7, 2018, 5:58pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035/2 "2018-06-07T17:58:46Z")

</div>

You have to use event.get/set now. And in case you ever have non-numerics in the set...

```auto
            kv = event.get('kvpairs')
            kv.to_hash.keys.each { |k|
                if kv[k].to_i.to_s == kv[k]
                    kv[k] = kv[k].to_i
                end
                if kv[k].to_f.to_s == kv[k]
                    kv[k] = kv[k].to_f
                end
            }
            event.set('kvpairs', kv)
        "

```

---

<div class="post-metadata">

**Author:** ![oleg31337](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@oleg31337](https://discuss.elastic.co/u/oleg31337)\
**Post date:** [June 7, 2018, 6:36pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035/3 "2018-06-07T18:36:13Z")

</div>

Thanks @Badger !  
But now there is another error:

Ruby exception occurred: undefined method `to\_hash' for nil:NilClass

---

<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:** [June 7, 2018, 6:38pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035/4 "2018-06-07T18:38:16Z")

</div>

Is the field actually called kvpairs? Doing a event.get on that returned nil.

---

<div class="post-metadata">

**Author:** ![oleg31337](https://avatars.discourse-cdn.com/v4/letter/o/3bc359/32.png) [@oleg31337](https://discuss.elastic.co/u/oleg31337)\
**Post date:** [June 7, 2018, 6:40pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035/5 "2018-06-07T18:40:31Z")

</div>

Yes it was. I have found what was wrong, instead of **to\_hash** now it should be **to\_h**

---

<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:** [July 5, 2018, 6:45pm UTC](https://discuss.elastic.co/t/convert-values-in-kv-pairs-using-ruby-filter/135035/6 "2018-07-05T18:45:46Z")

</div>

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