# How to convert all field names starting with string to integer

**URL:** https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728
**Category:** Logstash
**Created:** [March 25, 2022, 8:37pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728 "2022-03-25T20:37:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hagaluly](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@hagaluly](https://discuss.elastic.co/u/hagaluly)
#### Post date: [March 25, 2022, 8:37pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728/1 "2022-03-25T20:37:14Z")

</div>

i have tried this block without success  
i haven't find a proper way how to use wildcards

```auto
filter {
   if [fieldname] =~ /^STAGED.*/ {
        mutate {
            convert => {"[fieldname]" => "integer"}
        }
    }
}

```

---

<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 25, 2022, 11:11pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728/2 "2022-03-25T23:11:34Z")

</div>

You would need to use a ruby filter. If the fields are all at the top-level, like

```
{ "Foo.1": "1", "Bar.1": "1", "Bar.2": "a", "Baz.1": "1" }

```

then you could use

```
    ruby {
        code => '
            event.to_hash.each { |k, v|
                if k.start_with?("Bar")
                    event.set(k, v.to_i)
                end
            }
        '
    }

```

which produces

```
     "Baz.1" => "1",
     "Bar.2" => 0,
     "Bar.1" => 1,
     "Foo.1" => "1",

```

If you need to iterate through the fields of the events, as you would for

```
{ "anArray": [{ "Foo.1": "1", "Bar.1": "1" }, { "Foo.1": "1", "Bar.1": "1" }] }

```

you could try something like

```
    ruby {
        init => '
            def doSomething(object, name, keys, event)
            puts "doSomething called for #{name}"
                if object
                    if object.kind_of?(Hash) and object != {}
                        object.each { |k, v| doSomething(v, "#{name}[#{k}]", keys, event) }
                    elsif object.kind_of?(Array) and object != []
                        object.each_index { |i|
                            doSomething(object[i], "#{name}[#{i}]", keys, event)
                        }
                    else
                        # name is something like "[anArray][0][Bar.1]"
                        lastElement = name.gsub(/^.*\[/, "").gsub(/\]$/, "")
                        if lastElement.start_with? "Bar"
                            event.set(name, object.to_i)
                        end
                    end
                end
            end
        '
        code => '
            event.to_hash.each { |k, v|
                doSomething(v, "[#{k}]", @field, event)
            }
        '
    }

```

which produces

```
   "anArray" => [
    [0] {
        "Bar.1" => 1,
        "Foo.1" => "1"
    },
    [1] {
        "Bar.1" => 1,
        "Foo.1" => "1"
    }
],
```

---

<div class="post-metadata">

### Author: ![hagaluly](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@hagaluly](https://discuss.elastic.co/u/hagaluly)
#### Post date: [March 26, 2022, 7:06pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728/3 "2022-03-26T19:06:33Z")

</div>

i have a key value file.

STAGED\_stage1=34534  
STAGED\_stage2=2456  
STAGED\_stage3=6245  
STAGED\_stage4=45678

i need to scan the file for every key starting with STAGED and change it to integer

---

<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 26, 2022, 7:20pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728/4 "2022-03-26T19:20:05Z")

</div>

OK, so the first solution should work for you.

---

<div class="post-metadata">

### Author: ![hagaluly](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@hagaluly](https://discuss.elastic.co/u/hagaluly)
#### Post date: [March 26, 2022, 7:40pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728/5 "2022-03-26T19:40:36Z")

</div>

god dammit it worked!!!!!!!!!!  
can i ask another question?  
if i need to add or like

if X or Y or Z how can i do this in ruby?

thank you so much for your help i really appriciate it!!!

---

<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 23, 2022, 7:41pm UTC](https://discuss.elastic.co/t/how-to-convert-all-field-names-starting-with-string-to-integer/300728/6 "2022-04-23T19:41:17Z")

</div>

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