# Renaming fields dinamically

**URL:** <https://discuss.elastic.co/t/renaming-fields-dinamically/288237>\
**Category:** Logstash\
**Created:** [November 2, 2021, 3:16pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237 "2021-11-02T15:16:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElasticLiver](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticliver/32/64869_2.png) [@ElasticLiver](https://discuss.elastic.co/u/ElasticLiver)\
**Post date:** [November 2, 2021, 3:16pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237/1 "2021-11-02T15:16:24Z")

</div>

Hi, I got multiple fields with composed names likes the ones below

```auto
records.properties.multiVmGroupId
records.properties.rpoInSeconds

```

I want to get rid of the `records.properties` part, and left only the last word.

is there a way to do this dinamically, without using mutate-rename?

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [November 2, 2021, 3:35pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237/2 "2021-11-02T15:35:40Z")

</div>

You can do this with Ruby. You loop through `records.properties` and get the Field and Value and then write it back to the root level. Then delete `records`.

**Conf**

```auto
input { generator { lines => ['{ "records": { "properties": { "multiVmGroupId": 123, "rpoInSeconds": 456 }}}'] count => 1 codec => "json" } }
filter {
 ruby {
  code => '
   event.get("[records][properties]").each { |k, v|
    event.set(k,v)
   }
   event.remove("[records]")
  '
 }
}    
output { stdout { codec => "json" } }

```

**Output**

```auto
{
    "multiVmGroupId": 123,
    "rpoInSeconds": 456
}

```

---

<div class="post-metadata">

**Author:** ![ElasticLiver](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticliver/32/64869_2.png) [@ElasticLiver](https://discuss.elastic.co/u/ElasticLiver)\
**Post date:** [November 2, 2021, 4:08pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237/3 "2021-11-02T16:08:15Z")

</div>

Hi @aaron-nimocks  
Thanks for you answer, I got a \_rubyexception, I think because is just a string with dots, not a nested field, how can i do this if is just an string with dots?

---

<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:** [November 2, 2021, 4:10pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237/4 "2021-11-02T16:10:41Z")

</div>

You would replace the contents of the .each loop with something like [this](https://discuss.elastic.co/t/how-to-remove-part-of-name-in-multiple-fields-with-ruby-in-logstash/254263/5).

---

<div class="post-metadata">

**Author:** ![ElasticLiver](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elasticliver/32/64869_2.png) [@ElasticLiver](https://discuss.elastic.co/u/ElasticLiver)\
**Post date:** [November 2, 2021, 5:57pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237/5 "2021-11-02T17:57:12Z")

</div>

Tanks Badger, it work, with a litle change....have to pass the regular expression as argument otherwise gives me an ruby exception

```auto
        event.to_hash.each { |k,v|
            keymatch ="records.properties"
            if k =~ /#{keymatch}/
                newK = k.sub(/^records.properties./, "")
                event.remove(k)
                event.set(newK, v)
            end
        }

```

---

<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:** [November 30, 2021, 5:57pm UTC](https://discuss.elastic.co/t/renaming-fields-dinamically/288237/6 "2021-11-30T17:57:53Z")

</div>

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