Delete dictionary from array if one key is empty

Hi!

It's been a while since I last wrestled with logstash, and I can feel the rust!

I'm trying to delete an array element with delete_if, and I have trouble with it.

The element itself is a dictionary, and the condition to delete it is that one of the entries of this dictionary must be empty (or nil).

The data I get is a dictionary, data, which contains several key-value pairs. One of them, features, is itself a list of dictionaries. Said dictionaries contain several key-value pairs, one of which, geometry, is a dictionary containing two key-value pairs: type and coordinates.

data
{
  key_1: val_1
  features: [
    {
      key_1: val_1
	  geometry: {
	    type: "MultiPoint"
		coordinates: [
		  1.3498...
		  43.349...
		]
	  }
	  ...
	  key_n: val:n
	},
	{...},
	...
	{...}
  ]
  ...
  key_n: val_n
}

I need to delete those features which geometry is empty.

This is what I tried:

event.set('data_feat', event.get('[data][features]').delete_if {|a| ! a[geometry][type]})
event.set('[data][features]', 'data_feat')

Got this error:

"Ruby exception occurred: undefined local variable or method `geometry' ..."

What am I doing wrong? Any ideas?

Thank you in advance!

Hi,

try with this code:

event.set('data_feat', event.get('[data][features]').delete_if {|a| !a['geometry']['type']})
event.set('[data][features]', event.get('data_feat'))

Regards

Thank you, yago82!

This did the trick!

I had to change the test, since checking for the (non) existence of field [type] inside of [geometry] gave me a new error:

Ruby exception occurred: undefined method `[]' for nil:NilClass {:class=>"NoMethodError", :backtrace=>["(ruby filter code):6:in `block in filter_method'", "org/jruby/RubyArray.java:2869:in `delete_if'", ...

So I changed the test to a['geometry'] == nil and it worked perfectly!!

Than you!

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