# Rename nested field in Logstash using Ruby filter

**URL:** <https://discuss.elastic.co/t/rename-nested-field-in-logstash-using-ruby-filter/339637>\
**Category:** Logstash\
**Created:** [July 31, 2023, 3:44am UTC](https://discuss.elastic.co/t/rename-nested-field-in-logstash-using-ruby-filter/339637 "2023-07-31T03:44:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mario\_kazela](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mario_kazela/32/124314_2.png) [@mario\_kazela](https://discuss.elastic.co/u/mario_kazela)\
**Post date:** [July 31, 2023, 3:44am UTC](https://discuss.elastic.co/t/rename-nested-field-in-logstash-using-ruby-filter/339637/1 "2023-07-31T03:44:46Z")

</div>

Hi,

I have some issues with rename a nested field in json.

Example of nested field:  
`test_results.result.legacy.entities.user_mentions.name` then i want to rename it to `displayname`

I have try this method, but unfortunately, it only rename the field to `test_results.result.legacy.entities.user_mentions.namedisplay` instead of `namedisplay` only

Example of method:

```auto
ruby {
  code => "
    user_mentions = event.get('[test_results][result][legacy][entities][user_mentions]')
    if user_mentions
      b = []
      user_mentions.each { |k|
        k['namedisplay'] = k['name']
        k.delete('name')
        logger.info('for each k', 'value' => k)
        b << k
      }
      event.set('[test_results][result][legacy][entities][user_mentions]', b)
    end
  "
}

```

Really appreciated with all your help. Thanks

---

<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:** [July 31, 2023, 3:57am UTC](https://discuss.elastic.co/t/rename-nested-field-in-logstash-using-ruby-filter/339637/2 "2023-07-31T03:57:52Z")

</div>

> [@mario\_kazela](#):
>
> ```auto
> user_mentions.each { |k|
> k['namedisplay'] = k['name']
> 
> ```

The first line iterates over each member (k) of user\_mentions, which is the value of `[test_results][result][legacy][entities][user_mentions]`. So you are setting `[test_results][result][legacy][entities][user_mentions][namedisplay]` to the value of `[test_results][result][legacy][entities][user_mentions][name]`.

Perhaps you should change the second line to

```
event.set("namedisplay", k["name"])

```

but if [user\_mentions] is an array then it seems unlikely that that would be right.

---

<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:** [August 28, 2023, 3:58am UTC](https://discuss.elastic.co/t/rename-nested-field-in-logstash-using-ruby-filter/339637/3 "2023-08-28T03:58:04Z")

</div>

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