# Append nested variable's value to a string

**URL:** <https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470>\
**Category:** Logstash\
**Created:** [September 29, 2021, 12:03pm UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470 "2021-09-29T12:03:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Voolkan](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Voolkan](https://discuss.elastic.co/u/Voolkan)\
**Post date:** [September 29, 2021, 12:03pm UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/1 "2021-09-29T12:03:52Z")

</div>

Hello,  
I'm trying to setup a bit of a dynamic solution to the logstash config, and especially the json filter part. When setting the json part, I need to provide a source and a target fields. With source it's easy, however, the target field should be a bit dynamic, i.e in my case, based on another field value. For example, I have a nested field [kubernetes][container][name], and the following json configuration:

```auto
              json {
                source => "message"
                target => "message_json_%{[kubernetes][container][name]}"
                skip_on_invalid_json => true
              }

```

As you can see, I'm trying to append the nested value to the string in the target, however, it seems that no matter how I will mutate my target, it's always results in errors.  
Can someone please help me figure this one out?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Voolkan](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Voolkan](https://discuss.elastic.co/u/Voolkan)\
**Post date:** [September 30, 2021, 8:37am UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/2 "2021-09-30T08:37:34Z")

</div>

Anyone?

---

<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:** [September 30, 2021, 2:44pm UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/3 "2021-09-30T14:44:15Z")

</div>

The filter [does not sprintf](https://github.com/logstash-plugins/logstash-filter-json/blob/4d54cceab462a6e8f39d48bd72beba9b796a97be/lib/logstash/filters/json.rb#L93) the value of the target option, so you cannot do this directly. You would have to use a constant field name for the target (possibly under @metadata) and then use [mutate+rename](https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-rename).

---

<div class="post-metadata">

**Author:** ![Voolkan](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Voolkan](https://discuss.elastic.co/u/Voolkan)\
**Post date:** [October 1, 2021, 11:09am UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/4 "2021-10-01T11:09:33Z")

</div>

Hey Badger, thanks for the info. Can you perhaps help me out with an example on how to do so? I'm a bit lost in it.

---

<div class="post-metadata">

**Author:** ![Voolkan](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Voolkan](https://discuss.elastic.co/u/Voolkan)\
**Post date:** [October 4, 2021, 11:37am UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/5 "2021-10-04T11:37:22Z")

</div>

Bumping up again.  
@Badger hope you can help with this, thank you in advance!

---

<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:** [October 4, 2021, 2:03pm UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/6 "2021-10-04T14:03:20Z")

</div>

Use

```
target => "foo"

```

and then

```
mutate { rename => { "foo" => "message_json_%{[kubernetes][container][name]}" } }
```

---

<div class="post-metadata">

**Author:** ![Voolkan](https://avatars.discourse-cdn.com/v4/letter/v/94ad74/32.png) [@Voolkan](https://discuss.elastic.co/u/Voolkan)\
**Post date:** [October 8, 2021, 3:10pm UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/7 "2021-10-08T15:10:49Z")

</div>

Thanks a lot @Badger, worked like a charm!

---

<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 5, 2021, 3:10pm UTC](https://discuss.elastic.co/t/append-nested-variables-value-to-a-string/285470/8 "2021-11-05T15:10:57Z")

</div>

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