Generate an array of fields from other fields in an Array of Hashes


#1

Hello, I am trying to grab fields out of an array of hashes in Logstash. Right now it looks like this:

"hashes" => [
   [0] {
     "key1" => "value1",
     "key2"=> "value2",
     "key3" => "value3",
   }
   [1] {
     "key1" => "value1",
     "key2"=> "value2",
     "key3" => "value3",
   }
   [2] {
     "key1" => "value1",
     "key2"=> "value2",
     "key3" => "value3",
   }
]

I want to create an array with the fields of all the key2 values from each hash in the array. What would be the best way of going about this?

Right now I am using the ruby filter as such:

ruby {
   code => '
     hashes = event.get("hashes")
     vals= Array.new
     hashes.each {
       |c| vals.push("#{c[key2]}")
     }
     event.set("vals",vals)
   '
   add_tag => [ "foo"]
}

However, this does not seem to work. Also in the output, there is a _rubyexception tag. So my ruby is wrong but I am not sure where. Am I going about this the right way? And what is wrong with my ruby?

Thanks


#2

Works now. just needed to add quotes around "key2". That line now looks like this:

|c| vals.push(c["key2"])


(system) #3

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