# Set a nested field with a variable

**URL:** <https://discuss.elastic.co/t/set-a-nested-field-with-a-variable/258153>\
**Category:** Logstash\
**Created:** [December 9, 2020, 3:46pm UTC](https://discuss.elastic.co/t/set-a-nested-field-with-a-variable/258153 "2020-12-09T15:46:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwatbmc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dwatbmc/32/80518_2.png) [@dwatbmc](https://discuss.elastic.co/u/dwatbmc)\
**Post date:** [December 9, 2020, 3:46pm UTC](https://discuss.elastic.co/t/set-a-nested-field-with-a-variable/258153/1 "2020-12-09T15:46:57Z")

</div>

Can't find a way to do this with Mutate, so I am attempting it in Ruby, but still failing:

I have two fields:  
[log][group] = "panos"  
[log][type] = "threat"

Based on these two field values, I want to copy the JSON Object from [labels][group] to a new field [panos][threat], using the values in log.group and log.type, as they can change.

With Mutate:

```auto
mutate { add_field => {"[%{[log][group]}][%{[log][type]}]" => "%{[labels][group]}"} }

```

This results in a JSON String, rather than the nested object structure. So using Ruby:

```auto
code => '
                    mygroup = event.get("[log][group]").to_s
                    mytype = event.get("[log][type]").to_s
                    myvalue = event.get("[labels][group]")
                    event.set("[mygroup][mytype]",myvalue)
                '

```

The last line with event.set - I can't figure out the syntax to reference the two variables to create the nested field.

Ideas?

---

<div class="post-metadata">

**Author:** ![dwatbmc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dwatbmc/32/80518_2.png) [@dwatbmc](https://discuss.elastic.co/u/dwatbmc)\
**Post date:** [December 9, 2020, 4:00pm UTC](https://discuss.elastic.co/t/set-a-nested-field-with-a-variable/258153/2 "2020-12-09T16:00:29Z")

</div>

Found my own answer. For anyone else, the correct syntax:

```auto
event.set("[#{mygroup}][#{mytype}]",myvalue)

```

---

<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:** [January 6, 2021, 4:00pm UTC](https://discuss.elastic.co/t/set-a-nested-field-with-a-variable/258153/3 "2021-01-06T16:00:34Z")

</div>

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