# Can convert "10%" string to float?

**URL:** https://discuss.elastic.co/t/can-convert-10-string-to-float/125574
**Category:** Logstash
**Created:** [March 26, 2018, 9:26am UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574 "2018-03-26T09:26:10Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 9:26am UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/1 "2018-03-26T09:26:11Z")

</div>

Hello,  
It is possible to convert with filter logstash a string value like "10%" to float?

thnx.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 26, 2018, 11:39am UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/2 "2018-03-26T11:39:24Z")

</div>

Yes, you can do it with a ruby filter. This probably works:

```
event.set('fieldname', event.get('fieldname').gsub('%', '').to_f / 100)
```

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 1:08pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/3 "2018-03-26T13:08:42Z")

</div>

my json data was like this `{ "payload" : { "humidity" : { "Id" : "Sensor_DHT", "Model" : "DHT11", "value" : "10%" } } }`

can i use for 'fieldname' this '[payload][humidity][value]' ?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 26, 2018, 1:20pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/4 "2018-03-26T13:20:05Z")

</div>

Why don't you try it out?

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 1:25pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/5 "2018-03-26T13:25:00Z")

</div>

I have tried, but it doesn't work.  
this is the code than i have tryed with your suggestion  
`ruby{ code => "event.set('[payload][humidity][value]', event.get('[payload][humidity][value]').gsub('%', '').to_f / 100)" }`

I have tried even this code  
`mutate { gsub => ["[payload][humidity][value]", "%", "." ] } mutate { convert => ["[payload][humidity][value]", "float" ] }`  
I have searched much in the web but don't know any other way to solve this.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 26, 2018, 2:00pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/6 "2018-03-26T14:00:35Z")

</div>

"It doesn't work" isn't a useful error description. What happens? What does the resulting document look like? Copy/paste from Kibana's JSON tab.

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 2:05pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/7 "2018-03-26T14:05:33Z")

</div>

for this code  
`ruby{ code => "event.set('[payload][humidity][value]', event.get('[payload][humidity][value]').gsub('%', '').to_f /100)" }`  
it shows me this error  
`[2018-03-26T16:08:13,745][INFO][logstash.agent] Successfully started Logstash API endpoint {:port=>9600} SyntaxError: (ruby filter code):1: syntax error, unexpected tREGEXP_BEG @codeblock = lambda { |event, &new_event_block| event.set('[payload][humidity][v alue]', event.get('[payload][humidity][value]').gsub('%', '').to_f /100) }^ eval at org/jruby/RubyKernel.java:1079 register at C:/Users/user/Desktop/Elastic/logstash-5.6.4/vendor/bundle /jruby/1.9/gems/logstash-filter-ruby-3.0.4/lib/logstash/filters/ruby.rb:38`

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 2:15pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/8 "2018-03-26T14:15:36Z")

</div>

but for this code  
`ruby{ code => " test = event.get('[payload][humidity][value]').gsub('%', '').to_f / 100 event.set('[payload][humidity][value]', test)" }`

it shows me this error  
`[2018-03-26T16:13:40.921000 #7272] DEBUG -- : MONGODB | localhost:27017 | mon go_elastic.find | SUCCEEDED | 0.007s [2018-03-26T16:13:40,970][ERROR][logstash.filters.ruby] Ruby exception occur red: undefined method 'gsub' for nil:NilClass`

for this configuration it entered all my data but it does not switch the % character and doesn't convert it to float.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 26, 2018, 2:34pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/9 "2018-03-26T14:34:25Z")

</div>

> [logstash.filters.ruby] Ruby exception occur red: undefined method 'gsub' for nil:NilClass

So `event.get('[payload][humidity][value]')` returned nil. This happens when the field doesn't exist.

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 2:37pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/10 "2018-03-26T14:37:08Z")

</div>

Ok, i understand, so i think to use if statement to check if '[payload][humidity][value]' exist or not. Do you suggest another method?

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 26, 2018, 2:43pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/11 "2018-03-26T14:43:25Z")

</div>

now i used this code  
`if [payload][humidity][value]{ ruby{ code => " test = event.get('[payload][humidity][value]').gsub('%', '').to_f / 100 event.set('[payload][humidity][value]', test)" } }`  
it doesn't show any error and does not convert [payload][humidity][value] to float.

![Capture](https://us1.discourse-cdn.com/elastic/original/3X/5/2/52fe23066a7bf92de92c29abf73826faf4913ff4.PNG)

on kibana it was like this `"payload_humidity_value": "10%",`

---

<div class="post-metadata">

### Author: ![EditAxha](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@EditAxha](https://discuss.elastic.co/u/EditAxha)
#### Post date: [March 27, 2018, 12:41pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/12 "2018-03-27T12:41:40Z")

</div>

I have solved the problem using this code:

`if [payload_humidity_value] { mutate { gsub => ["payload_humidity_value", "%", ""] convert => ["payload_humidity_value", "float"] } }`

Seems like the problem was at the syntax. Calling the field in this way `gsub => ["payload_humidity_value", "%", ""]` solved it.

Notice that i am using Logstash\_input\_mongodb plugin to get data from MongoDB.

---

<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: [April 24, 2018, 12:45pm UTC](https://discuss.elastic.co/t/can-convert-10-string-to-float/125574/13 "2018-04-24T12:45:42Z")

</div>

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