Help please.. to acchive json format

I have rosterentry field as bellow
rosterentry = "100,01-JUL-20,01-AUG-22|100,02-JUL-20,02-AUG-22|101,03-JUL-19,03-AUG-19"

I have converted to :

    rosterentry = [
               "100,01-JUL-20,01-AUG-22",
                "100,02-JUL-20,02-AUG-22",
                "101,03-JUL-19,03-AUG-19"
              ]

Wants to convert in bellow format:

"rosterentry": [
                {
                    "rolerosterid": 100,
                    "rosterdaterange": [
                        {
                            "rolerosterstartdate": "01-JUL-20",
                            "rolerosterenddate": "01-AUG-22"
                        },
                        {
                            "rolerosterstartdate": "02-JUL-20",
                            "rolerosterenddate": "02-AUG-22"
                        }
                    ]
                },
                {
                    "rolerosterid": 101,
                    "rosterdaterange": [
                        {
                            "rolerosterstartdate": "03-JUL-19",
                            "rolerosterenddate": "03-AUG-19"
                        }
                    ]
                }
             ]

Any help will be appreciated

You could do that with ruby

    ruby {
        code => '
            roster = event.get("rosterentry")
            if roster.is_a? Array
                rosterHash = {}
                roster.each { |x|
                    entry = x.split(",")
                    if entry.length == 3
                        id = entry[0]
                        startDate = entry[1]
                        endDate = entry[2]
                        if !rosterHash[id]
                            rosterHash[id] = { "rolerosterid" => id, "rosterdaterange" => [] }
                        end
                        rosterHash[id]["rosterdaterange"] << { "rolerosterstartdate" => startDate, "rolerosterenddate" => endDate }
                    end
                }
            end
            rosterArray = []
            rosterHash.each { |k, v|
                rosterArray << v
            }
            event.set("rosterentry", rosterArray)
        '
    }
1 Like

Thank you @Badger

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