# Dynamic split field and nested loops

**URL:** <https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808>\
**Category:** Logstash\
**Created:** [January 18, 2019, 2:10pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808 "2019-01-18T14:10:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fadihaddad](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fadihaddad](https://discuss.elastic.co/u/fadihaddad)\
**Post date:** [January 18, 2019, 2:10pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/1 "2019-01-18T14:10:38Z")

</div>

I have this Field "dAList" : "8~275273~273586~1687~20181209|23~2660000~2660000~0~20181209"

each part separated by "|" is a Da Number i want to split it first to display for example Da\_1,Da\_2 inside the dAList and the values separated by "~" to be nested inside each Da number for example:

dAList is separated to other fields  
and each filed has DA\_1 {  
DA\_a : 8  
DA\_b : 275273  
DA\_c : 273586  
DA\_d : 1687  
DA\_e : 20181209  
}  
DA\_2{  
DA\_ID: 23  
etc  
}

I tried to use this ruby filter but gave me exception  
ruby {  
code =\> "  
event['DAnumbers'] = event['dAList'].split('|').collect { |t|  
c = t.split '~'  
{  
'DA\_ID' =\> c[0],  
'DA\_Before' =\> c[1],  
'DA\_After' =\> c[2],  
'DA\_Change' =\> c[3],  
'DA\_ExpDate' =\> c[4],  
}  
}  
"  
}

---

<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 18, 2019, 3:21pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/2 "2019-01-18T15:21:08Z")

</div>

Unless you are running a very old version, you cannot do direct field references. Use [event.get and .set](https://www.elastic.co/guide/en/logstash/current/event-api.html).

```
        x = event.get('dAList').split('|').collect { |t|
            c = t.split '~'
            {
            'DA_ID' => c[0],
            'DA_Before' => c[1],
            'DA_After' => c[2],
            'DA_Change' => c[3],
            'DA_ExpDate' => c[4],
            }
        }
        event.set('DAnumbers', x)

```

That will get you

```
 "DAnumbers" => [
    [0] {
             "DA_ID" => "8",
         "DA_Before" => "275273",
         "DA_Change" => "1687",
          "DA_After" => "273586",
        "DA_ExpDate" => "20181209"
    },
    [1] {
             "DA_ID" => "23",
         "DA_Before" => "2660000",
         "DA_Change" => "0",
          "DA_After" => "2660000",
        "DA_ExpDate" => "20181209"
    }
],
```

---

<div class="post-metadata">

**Author:** ![fadihaddad](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fadihaddad](https://discuss.elastic.co/u/fadihaddad)\
**Post date:** [January 18, 2019, 3:37pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/3 "2019-01-18T15:37:52Z")

</div>

Hello, Thank you so much for your reply! it fixed my problem, I also wanted to how to convert the type of the nested fields like DA\_ExpDate to a date type or DA\_Before to integer

---

<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 18, 2019, 3:43pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/4 "2019-01-18T15:43:36Z")

</div>

```
            'DA_ID' => c[0],
            'DA_Before' => c[1].to_i,
            'DA_After' => c[2].to_i,
            'DA_Change' => c[3],
            'DA_ExpDate' => Time.strptime(c[4], '%Y%m%d')

```

Error handling is left as an exercise for the reader.

---

<div class="post-metadata">

**Author:** ![fadihaddad](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fadihaddad](https://discuss.elastic.co/u/fadihaddad)\
**Post date:** [January 18, 2019, 10:57pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/5 "2019-01-18T22:57:50Z")

</div>

Thank you so much you fixed my problem

---

<div class="post-metadata">

**Author:** ![fadihaddad](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@fadihaddad](https://discuss.elastic.co/u/fadihaddad)\
**Post date:** [January 21, 2019, 2:49pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/6 "2019-01-21T14:49:30Z")

</div>

hello, sorry for asking too much, but how can I set the timezone for the ExpDate

---

<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 18, 2019, 2:49pm UTC](https://discuss.elastic.co/t/dynamic-split-field-and-nested-loops/164808/7 "2019-02-18T14:49:46Z")

</div>

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