# Delete dictionary from array if one key is empty

**URL:** https://discuss.elastic.co/t/delete-dictionary-from-array-if-one-key-is-empty/351863
**Category:** Logstash
**Created:** [January 26, 2024, 9:17am UTC](https://discuss.elastic.co/t/delete-dictionary-from-array-if-one-key-is-empty/351863 "2024-01-26T09:17:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ITIC](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ITIC](https://discuss.elastic.co/u/ITIC)
#### Post date: [January 26, 2024, 9:17am UTC](https://discuss.elastic.co/t/delete-dictionary-from-array-if-one-key-is-empty/351863/1 "2024-01-26T09:17:38Z")

</div>

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`.

```auto
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:

```auto
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!

---

<div class="post-metadata">

### Author: ![yago82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yago82/32/97755_2.png) [@yago82](https://discuss.elastic.co/u/yago82)
#### Post date: [January 26, 2024, 1:01pm UTC](https://discuss.elastic.co/t/delete-dictionary-from-array-if-one-key-is-empty/351863/2 "2024-01-26T13:01:30Z")

</div>

Hi,

try with this code:

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

```

Regards

---

<div class="post-metadata">

### Author: ![ITIC](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ITIC](https://discuss.elastic.co/u/ITIC)
#### Post date: [January 29, 2024, 1:08pm UTC](https://discuss.elastic.co/t/delete-dictionary-from-array-if-one-key-is-empty/351863/3 "2024-01-29T13:08:00Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 26, 2024, 1:08pm UTC](https://discuss.elastic.co/t/delete-dictionary-from-array-if-one-key-is-empty/351863/4 "2024-02-26T13:08:54Z")

</div>

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