# 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:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![aerodynamic](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@aerodynamic](https://discuss.elastic.co/u/aerodynamic)
#### Post date: [March 7, 2022, 6:45pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/1 "2022-03-07T18:45:38Z")

</div>

Currently I'm dealing with a situation where a field name is dynamically created and needs to be set to an integer like so

```auto
mutate {
      add_field => {
         "%{fieldName}" => "%{dataInteger}"
      }
      convert => { "%{fieldName}" => "integer" }
}  

```

This conversion does not affect fieldName.

---

<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, 6:58pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/2 "2022-03-07T18:58:31Z")

</div>

mutate does operations in a [specific order](https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-proc_order), and the common options like add\_field come [after that](https://github.com/elastic/logstash/blob/0887d7609df9ba72a5d0cd003c639b16da198622/logstash-core/lib/logstash/filters/base.rb#L197). Thus [fieldName] is not created until after the convert, and convert of a non-existent field is a no-op. Split it into two mutate filters.

---

<div class="post-metadata">

### Author: ![aerodynamic](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@aerodynamic](https://discuss.elastic.co/u/aerodynamic)
#### Post date: [March 7, 2022, 7:13pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/3 "2022-03-07T19:13:44Z")

</div>

Hello @Badger

Thanks for the response!

We have tried

```auto

mutate {
      add_field => {
         "%{fieldName}" => "%{dataInteger}"
      }

}
mutate {
      convert => { "%{fieldName}" => "integer" }
}

```

But it still doesn't function

---

<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, 7:46pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/4 "2022-03-07T19:46:48Z")

</div>

I suspect what you want is

```
mutate { add_field => { "fieldName" => "%{dataInteger}" } }
mutate { convert => { "fieldName" => "integer" } }
```

---

<div class="post-metadata">

### Author: ![aerodynamic](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@aerodynamic](https://discuss.elastic.co/u/aerodynamic)
#### Post date: [March 7, 2022, 8:15pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/5 "2022-03-07T20:15:04Z")

</div>

To be clear.

fieldName is the name of a variable who's value is the actual field name.

Like at some point

```auto
fieldName => "cookieCount"

```

Does that make sense?

---

<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

```

---

<div class="post-metadata">

### Author: ![aerodynamic](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@aerodynamic](https://discuss.elastic.co/u/aerodynamic)
#### Post date: [March 7, 2022, 10:00pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/7 "2022-03-07T22:00:25Z")

</div>

Thank you for the reply and explanation!

I will try ASAP

---

<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 4, 2022, 10:00pm UTC](https://discuss.elastic.co/t/cannot-convert-created-field-to-integer/299005/8 "2022-04-04T22:00:36Z")

</div>

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