# Logstash filter: Convert all fields in json to integer

**URL:** https://discuss.elastic.co/t/logstash-filter-convert-all-fields-in-json-to-integer/180662
**Category:** Logstash
**Created:** [May 11, 2019, 4:21pm UTC](https://discuss.elastic.co/t/logstash-filter-convert-all-fields-in-json-to-integer/180662 "2019-05-11T16:21:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Neeraj\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neeraj_jain/32/41289_2.png) [@Neeraj\_Jain](https://discuss.elastic.co/u/Neeraj_Jain)
#### Post date: [May 11, 2019, 4:21pm UTC](https://discuss.elastic.co/t/logstash-filter-convert-all-fields-in-json-to-integer/180662/1 "2019-05-11T16:21:58Z")

</div>

Hi,  
I have a json data of below format (in rubydebug output). I want to convert all the counter values to integer. with mutate filter plugin, i have to convert each counter to integer. However, this is a shortened data. i have a lot of counters. So i tried using the ruby code.. Ruby Code show below.  
I get error "Ruby exception occurred: no implicit conversion of String into Integer" for this ruby code. I dont think the error is for value.to\_i. its something to do with "event.set(['counters']['#{key}']".

> {  
> "@timestamp" =\> 2019-05-11T14:37:56.933Z,  
> "component-name" =\> "myComponent",  
> "counters" =\> {  
> "counter1" =\> "19034",  
> "counter2" =\> "90",  
> "counter3" =\> "46",  
> "counter4" =\> "11",  
> "counter5" =\> "0",  
> "counter6" =\> "0",  
> "counter7" =\> "32737",  
> },  
> "host" =\> "mylinushost",  
> "@version" =\> "1"  
> }

```
   ruby {
      code => "
          counterdata = event.get('[counters]')
          counterdata.to_hash
          counterdata.each { |key, value|
              event.set(['counters']['#{key}'], value.to_i)
          }
      "
  }

```

Please help. Or is there any other option apart from ruby. Thanks

---

<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: [May 11, 2019, 6:27pm UTC](https://discuss.elastic.co/t/logstash-filter-convert-all-fields-in-json-to-integer/180662/2 "2019-05-11T18:27:49Z")

</div>

Try

```
ruby {
    code => '
        event.get("[counters]").each { |key, value|
            event.set("[counters][#{key}]", value.to_i)
        }
    '
}
```

---

<div class="post-metadata">

### Author: ![Neeraj\_Jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/neeraj_jain/32/41289_2.png) [@Neeraj\_Jain](https://discuss.elastic.co/u/Neeraj_Jain)
#### Post date: [May 13, 2019, 6:41am UTC](https://discuss.elastic.co/t/logstash-filter-convert-all-fields-in-json-to-integer/180662/3 "2019-05-13T06:41:53Z")

</div>

Hi @Badger,  
Thanks.. That worked like a charm.

~Neeraj

---

<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: [June 10, 2019, 6:41am UTC](https://discuss.elastic.co/t/logstash-filter-convert-all-fields-in-json-to-integer/180662/4 "2019-06-10T06:41:57Z")

</div>

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