# Using "merge" to append a string literal to an array field?

**URL:** <https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353>\
**Category:** Logstash\
**Created:** [March 7, 2019, 4:34pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353 "2019-03-07T16:34:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlacuache](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tlacuache/32/39003_2.png) [@tlacuache](https://discuss.elastic.co/u/tlacuache)\
**Post date:** [March 7, 2019, 4:34pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353/1 "2019-03-07T16:34:28Z")

</div>

this is probably a stupid question, but how can I append a literal string to an array in logstash?

i tried merge:

```auto
if [my_ip] and ([my_ip] == "127.0.0.1") {
  mutate {
    merge => { "[my_hostname]" => "loopback" }
  }
}

```

but the issue is that it's looking for a field called "loopback", so my\_hostname ends up empty.

is there a way i can indicate the value is a literal rather than a field name?

i could probably do something like this:

```auto
event.set('my_hostname', event.get('my_hostname') + ['loopback'])

```

but I don't know if that will be worse performance?

Or i could add it to a temporary `@metadata` field and then merge that, but that seems like extra steps too.

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:** [March 7, 2019, 5:47pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353/2 "2019-03-07T17:47:53Z")

</div>

```
mutate { gsub => { "myhostname", "$", "loopback" } }
```

---

<div class="post-metadata">

**Author:** ![tlacuache](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tlacuache/32/39003_2.png) [@tlacuache](https://discuss.elastic.co/u/tlacuache)\
**Post date:** [March 7, 2019, 5:54pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353/3 "2019-03-07T17:54:06Z")

</div>

Not quite what I meant, I think. I'm not wanting to append "loopback" to the string in myhostname. I want myhostname to be an array, and I am appending "loopback" to its list of values.

In other words, prior to this "myhostname" might contain two elements, "apple" and "banana". After this filter, I want myhostname to contain three elements, "apple", "banana", and "loopback"

Or does gsub also intepret "$" to mean "the end of the array" if the field is an array field?

---

<div class="post-metadata">

**Author:** ![tlacuache](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tlacuache/32/39003_2.png) [@tlacuache](https://discuss.elastic.co/u/tlacuache)\
**Post date:** [March 7, 2019, 6:27pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353/4 "2019-03-07T18:27:55Z")

</div>

Hm, I'm reading now that apparently calling add\_field twice on the same field will turn it into an array, which would be simpler than using merge. I'll try that.

---

<div class="post-metadata">

**Author:** ![tlacuache](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tlacuache/32/39003_2.png) [@tlacuache](https://discuss.elastic.co/u/tlacuache)\
**Post date:** [March 8, 2019, 7:39pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353/5 "2019-03-08T19:39:46Z")

</div>

Yes, that works. Apparently calling "add\_field" twice turns the value into an array.

---

<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:** [April 5, 2019, 7:39pm UTC](https://discuss.elastic.co/t/using-merge-to-append-a-string-literal-to-an-array-field/171353/6 "2019-04-05T19:39:51Z")

</div>

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