# Convert array with 2 equal values to single value

**URL:** <https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763>\
**Category:** Logstash\
**Created:** [August 26, 2019, 12:06pm UTC](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763 "2019-08-26T12:06:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![programagor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/programagor/32/40183_2.png) [@programagor](https://discuss.elastic.co/u/programagor)\
**Post date:** [August 26, 2019, 12:06pm UTC](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763/1 "2019-08-26T12:06:10Z")

</div>

Greetings

My events sometimes have a field "foo" equal to a single value, sometimes an array of multiple distinct values, and sometimes an array of exactly two equal values.  
I would like to convert the last case to the first case.  
For example, I would like to convert this

```auto
"foo": [
  "bar",
  "bar"
],

```

to

```auto
"foo": "bar"

```

Is there an easy method to do this? I was thinking i could do something along those lines:

```auto
if foo[0] == foo[1]
  mutate {
    foo_tmp = foo[0]
    remove foo
    foo = foo_tmp
    remove foo_tmp
  }

```

but I am unsure about the exact syntax when it comes to array element access.

---

<div class="post-metadata">

**Author:** ![glenacota](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glenacota/32/41367_2.png) [@glenacota](https://discuss.elastic.co/u/glenacota)\
**Post date:** [August 26, 2019, 1:10pm UTC](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763/2 "2019-08-26T13:10:29Z")

</div>

> Edit after @Badger's comment

Hi, what do you think of:

```
ruby {
  code => '
     foo = event.get("foo")
     foo = foo.kind_of?(Array) && foo.uniq.length == 1 ? foo.uniq[0] : foo
     event.set("foo", foo)
  '
}

```

---

<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:** [August 26, 2019, 1:17pm UTC](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763/3 "2019-08-26T13:17:52Z")

</div>

That will convert ["bar", "bar"] to

```
       "foo" => [
    [0] "bar"
],

```

not

```
"foo" => "bar"

```

It also results in a ruby exception when [foo] is not an array, so you need some more code around it.

---

<div class="post-metadata">

**Author:** ![programagor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/programagor/32/40183_2.png) [@programagor](https://discuss.elastic.co/u/programagor)\
**Post date:** [August 26, 2019, 2:02pm UTC](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763/4 "2019-08-26T14:02:05Z")

</div>

Perfect, does exactly what I needed.

---

<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:** [September 23, 2019, 2:02pm UTC](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763/5 "2019-09-23T14:02:14Z")

</div>

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