# Logstash how to keep only last elements of an array?

**URL:** <https://discuss.elastic.co/t/logstash-how-to-keep-only-last-elements-of-an-array/304366>\
**Category:** Logstash\
**Created:** [May 10, 2022, 3:17pm UTC](https://discuss.elastic.co/t/logstash-how-to-keep-only-last-elements-of-an-array/304366 "2022-05-10T15:17:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zid57](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zid57/32/94548_2.png) [@zid57](https://discuss.elastic.co/u/zid57)\
**Post date:** [May 10, 2022, 3:17pm UTC](https://discuss.elastic.co/t/logstash-how-to-keep-only-last-elements-of-an-array/304366/1 "2022-05-10T15:17:48Z")

</div>

Hello all,

I have a field which is an array and I would like to keep only the last element and put it into a new field.

```auto
    "data": {
      "path": [
        1111,
        2222,
        3333
      ]

```

I tried to copy data.path into a new field and use gsub to keep only the last one.  
According Logstash logs. it seems that we cannot apply gsub on array.

```auto
[2022-05-10T17:02:41,062][WARN][logstash.filters.mutate][network-processing][376475260c6f8b36d68ccab991531238e7cf73a095a8a804165da9eaac7a6de5] gsub mutation is only applicable for strings and arrays of strings, skipping {:field=>"[as][origin]", :value=>1111}
[2022-05-10T17:02:41,063][WARN][logstash.filters.mutate][network-processing][376475260c6f8b36d68ccab991531238e7cf73a095a8a804165da9eaac7a6de5] gsub mutation is only applicable for strings and arrays of strings, skipping {:field=>"[as][origin]", :value=>2222}
[2022-05-10T17:02:41,063][WARN][logstash.filters.mutate][network-processing][376475260c6f8b36d68ccab991531238e7cf73a095a8a804165da9eaac7a6de5] gsub mutation is only applicable for strings and arrays of strings, skipping {:field=>"[as][origin]", :value=>3333}

```

I tried also to convert the new field into a string but still the same error.

```auto
 mutate {
      copy => { "[data][path]" => "[data][new_field]" }
    }

mutate {
      convert => { "[data][new_field]" => "string" }
      gsub => ["[data][new_field]", ".*\ ", "" ]
    }

```

I have the feeling that I'm not on the good way to achieve it.

Do someone has an idea how to proceed ?

Thank you,

Regards,

zid57

---

<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:** [May 10, 2022, 4:28pm UTC](https://discuss.elastic.co/t/logstash-how-to-keep-only-last-elements-of-an-array/304366/2 "2022-05-10T16:28:28Z")

</div>

You could do something like

```
input { generator { count => 1 lines => ['{ "a": [ 1, 2, 3] }' ] codec => json { target => "[document]" } } }
filter { mutate { replace => { "document" => "%{[document][a][-1]}" } } }
output { stdout { codec => rubydebug { metadata => false } } }

```

which will produce

```
  "document" => "3",
```

---

<div class="post-metadata">

**Author:** ![zid57](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zid57/32/94548_2.png) [@zid57](https://discuss.elastic.co/u/zid57)\
**Post date:** [May 16, 2022, 2:01pm UTC](https://discuss.elastic.co/t/logstash-how-to-keep-only-last-elements-of-an-array/304366/3 "2022-05-16T14:01:28Z")

</div>

Hello,

It works !  
Thank you very much !

Cheers,

zid57

---

<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:** [June 13, 2022, 2:02pm UTC](https://discuss.elastic.co/t/logstash-how-to-keep-only-last-elements-of-an-array/304366/4 "2022-06-13T14:02:12Z")

</div>

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