# How to loop through array in Logstash?

**URL:** <https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/291747>\
**Category:** Logstash\
**Created:** [December 14, 2021, 6:26am UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/291747 "2021-12-14T06:26:08Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [December 14, 2021, 6:34pm UTC](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/291747/2 "2021-12-14T18:34:26Z")

</div>

> [@rijo\_joseph](#):
>
> I need to loop through "contents" array and need to perform decrypt the field "rawResponse" if "eligible" == "true"

One option would be to use a split filter to split that event into multiple events which each have a [contents] field which is a hash rather than an array. If you need to, you can then [recombine](https://discuss.elastic.co/t/a-json-array-consists-of-objects-how-to-deal-with/264464/2) them into a single event.

The other option is to iterate over the array in a ruby filter.

```
ruby {
    code => '
        c = event.get("contents")
        if c.is_a? Array
            c.each { |x|
            if x["eligible"]
                resp = x["rawResponse"]
                value = ...
                event.set("rawResponse", value)
            end
        end
    '
}

```

You would have to figure out what to replace the ... with by repurposing code from the [cipher](https://github.com/logstash-plugins/logstash-filter-cipher/blob/main/lib/logstash/filters/cipher.rb) filter.

---

_[View the full topic](https://discuss.elastic.co/t/how-to-loop-through-array-in-logstash/291747)._
