Flatten json array to combine fields (right ruby code?))


(Samta Malhotra) #1

Here is how my input data looks like:

     {
     "details": [
      {
    "place": "abc",
    "group": 3,
     "year": 2006,
    "id": 1304,
      "street": "xyz 14",
       "lf_number": "0118",
     "code": 4433,
       "name": "abc coorperation",
    "group2": 3817"
    "group1": 32"
      "postal_code": "22926",
   "status": 2
     },
  {
   "place": "cbc",
      "group": 2,
     "year": 2007,
    "id": 4983,
    "street": "mnc 14",
     "lf_number": "0145",
    "code": 4433,
    "name": "abc coorperation", 
    "group2": 3817,
     "group1": 32"
    "postalcode": "22926",
      "status": 2
  }
  ],
    "p_name": "anacin",
    "@version": "1",
    "id": 28841
    }

I want to flatten this array more and combine various fields here. I am able to do that with one field but how can i use more than one field.

Here is what i have tried so far and it works for one field.

 filter{

            ruby {

     code => "if(!event.get('[details][0]').nil?)

         event.set('[status]',event.get('[details]').group_by { |m| {m['postal_code'],m['status']}}.join(','))
end"

 }

   }

This works only if i have one field. How can i group multiple fields here??


(Samta Malhotra) #2

I am able to join only two fields now. But i want them to be seperated by @, when there is more than two hashes in the array.

filter{

            ruby {

code => "if(!event.get('[details][0]').nil?)

value = ''

value = event.get('[details]').map { |m| '{m.values_at('id','name')}.join(',')' }.join('@')
event.set('test',value)
end"

}

}

This does not work as desired, please help to solve the problem.


(system) #3

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