Dynamic split field and nested loops

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],
}
}
"
}

Unless you are running a very old version, you cannot do direct field references. Use event.get and .set.

        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"
    }
],
1 Like

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

            '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.

Thank you so much you fixed my problem

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

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