# Puting multiple if conditionals into loop

**URL:** https://discuss.elastic.co/t/puting-multiple-if-conditionals-into-loop/261226
**Category:** Logstash
**Created:** [January 15, 2021, 8:14am UTC](https://discuss.elastic.co/t/puting-multiple-if-conditionals-into-loop/261226 "2021-01-15T08:14:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![woolfik](https://avatars.discourse-cdn.com/v4/letter/w/b9e5f3/32.png) [@woolfik](https://discuss.elastic.co/u/woolfik)
#### Post date: [January 15, 2021, 8:14am UTC](https://discuss.elastic.co/t/puting-multiple-if-conditionals-into-loop/261226/1 "2021-01-15T08:14:43Z")

</div>

Good morning.  
So I'm into logstash filters code which contains tens of similar if conditionals like following:

```auto
if ("123456" == [ID]) {
  mutate {
    add_field => {
      "[fields][key1]" => "value1"
      "[fields][key2]" => "value2"
    }
  }
}

```

I was thinking about using ruby and array of hashes:  
hashes = [  
{"ID" =\> "123456", "key1" =\> "value1", "key2" =\> "value2"},  
{"ID" =\> "234567", "key1" =\> "value1", "key2" =\> "value2"},  
{"ID" =\> "345678", "key1" =\> "value1", "key2" =\> "value2"},  
and so on ]

Is this a good approach and if it could I get any tips or links how to achieve that? Or if I took the wrong path could I get any advice how can achive my goal which is replacing tens if similar conditions with some friendly code?

---

<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: [January 15, 2021, 4:33pm UTC](https://discuss.elastic.co/t/puting-multiple-if-conditionals-into-loop/261226/2 "2021-01-15T16:33:24Z")

</div>

You could use a translate filter. [This](https://discuss.elastic.co/t/logstash-how-to-configure-csv-filter-for-joining-2-csv-files-based-on-a-common-field-mapping-one-to-many-data-and-ingest-the-data-into-single-index/259994/2) has an example that might help.

---

<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: [February 12, 2021, 4:33pm UTC](https://discuss.elastic.co/t/puting-multiple-if-conditionals-into-loop/261226/3 "2021-02-12T16:33:33Z")

</div>

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