# Cannot convert created field to integer

**URL:** https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005
**Category:** Logstash
**Created:** [March 7, 2022, 6:45pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005 "2022-03-07T18:45:38Z")
**Posts on this page:** 1
**Showing post:** 6

<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: [March 7, 2022, 8:45pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/6 "2022-03-07T20:45:17Z")

</div>

> [@aerodynamic](#):
>
> fieldName is the name of a variable who's value is the actual field name.

That will work for the add\_field because the [decorator calls sprintf](https://github.com/elastic/logstash/blob/0887d7609df9ba72a5d0cd003c639b16da198622/logstash-core/lib/logstash/util/decorators.rb#L31) on the fieldname. The convert function just does a [get](https://github.com/logstash-plugins/logstash-filter-mutate/blob/d45bfb7211fc0a11afd7dc917cefa295752cc63d/lib/logstash/filters/mutate.rb#L308) of the field. Since it does not sprintf you cannot use %{} in the convert.

You would have to use a ruby filter to do it. The following

```
input { generator { count => 1 lines => [''] } }
filter {
    mutate { add_field => { "fieldName" => "foo" "foo" => "1" } }
    ruby {
        code => '
            name = event.get("fieldName")
            event.set(name, event.get(name).to_i)
        '
    }
}
output { stdout { codec => rubydebug { metadata => false } } }

```

will produce

```
       "foo" => 1

```

---

_[View the full topic](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005)._
