# How to dynamic parse fieldname and replace?

**URL:** <https://discuss.elastic.co/t/how-to-dynamic-parse-fieldname-and-replace/179858>\
**Category:** Logstash\
**Created:** [May 6, 2019, 11:15pm UTC](https://discuss.elastic.co/t/how-to-dynamic-parse-fieldname-and-replace/179858 "2019-05-06T23:15:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ck1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ck1/32/45666_2.png) [@ck1](https://discuss.elastic.co/u/ck1)\
**Post date:** [May 6, 2019, 11:15pm UTC](https://discuss.elastic.co/t/how-to-dynamic-parse-fieldname-and-replace/179858/1 "2019-05-06T23:15:54Z")

</div>

Hello,

I am trying data that looks like this

unwanted.field1  
unwanted.field2  
categories.colors.blue.t.type

First I need to remove every unwanted field, then I want to change categories.colors to only blue.t.type.

I can use a kv filter for to remove unwanted fileds, but I am unable to find an easy way to remove  
catories.colors dynamically

I want

unwanted.field1  
unwanted.field2  
categories.colors.t.blue.type  
categories.colors.t.red.type

**to become**  
t.blue.type  
t.red.type

My code

//ruby {  
code =\> "  
begin  
keys = event.to\_hash.keys  
keys.each{|key|

# if ( key =~ /m./ )

```
         if key.start_with? '[categories][colors]'
          #I don't know what should go in the event.set, how do I can a field name matching a pattern and doing a substring to get everything after the ''.t.''
          event.set('key.substring something', event.remove(key))

```

I amd having a hard time doing a start, ruby doesnt seem to like [field][field] and I can't escape de dots either with a backslash..

I am starting to wonder if i am on the right path or is there an easier way?

Thank you,

---

<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 7, 2019, 2:37pm UTC](https://discuss.elastic.co/t/how-to-dynamic-parse-fieldname-and-replace/179858/2 "2019-05-07T14:37:36Z")

</div>

If you field names contain periods then you could use

```
    ruby {
        code => '
            event.to_hash.each { |k, v|
                if k.start_with?("unwanted")
                    event.remove(k)
                elsif k.start_with?("categories.colors.")
                    event.set(k.sub("categories.colors.", ""), v)
                    event.remove(k)
                end
            }
        '
    }

```

If they are objects then you do not need ruby.

```
    mutate {
        remove_field => ["unwanted", "[categories]" ]
        rename => { "categories][colors][t]" => "t" }
    }
```

---

<div class="post-metadata">

**Author:** ![ck1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ck1/32/45666_2.png) [@ck1](https://discuss.elastic.co/u/ck1)\
**Post date:** [May 23, 2019, 1:13pm UTC](https://discuss.elastic.co/t/how-to-dynamic-parse-fieldname-and-replace/179858/3 "2019-05-23T13:13:16Z")

</div>

Thakn you, I will try this out! Will post back with results

---

<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 20, 2019, 1:13pm UTC](https://discuss.elastic.co/t/how-to-dynamic-parse-fieldname-and-replace/179858/4 "2019-06-20T13:13:17Z")

</div>

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