Insert multiple fields in nested array

Hi Guys

I have the following input as example:

generator { count => 1 lines => [ '{ "RATING_GROUP": "7,843,13", "CONSUMO": "328994,29715,13948" }' ] codec => json }

Which filter can I use in Logstash to obtain a output like this one bellow ?

{
  "rating_group_consumo" : [ 
    {
      "rating_group" : "7",
      "consumo" :  "328994"
    },
    {
      "rating_group" : "843",
      "consumo" :  "29715"
    },
    {
      "rating_group" : "13",
      "consumo" :  "13948"
    }
  ]
}

You would have to do that in a ruby filter

    ruby {
        code => '
            consumo = event.get("CONSUMO")
            group = event.get("RATING_GROUP")
            begin
                consumo = consumo.split(",")
                group = group.split(",")
                a = []
                consumo.each_index { |x|
                    a << { "rating_group" => group[x], "consumo" => consumo[x] }
                }
                event.set("rating_group_consumo", a)
            rescue
            end
        '
    }
2 Likes

Thanks Badger !! It works perfectly :fist_right: :fist_left:

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