# How to use ruby filter

**URL:** https://discuss.elastic.co/t/how-to-use-ruby-filter/35555
**Category:** Logstash
**Created:** [November 25, 2015, 1:09pm UTC](https://discuss.elastic.co/t/how-to-use-ruby-filter/35555 "2015-11-25T13:09:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![happyeveryday](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@happyeveryday](https://discuss.elastic.co/u/happyeveryday)
#### Post date: [November 25, 2015, 1:09pm UTC](https://discuss.elastic.co/t/how-to-use-ruby-filter/35555/1 "2015-11-25T13:09:44Z")

</div>

My problem is specific as below.

the event message is:

```
   {"a":[{"aa":"v1"},{"bb":"v2"}]}

```

how to add a field ,namely, "a\_size" and its value is array a's length. so the output is like below:

`{"a":[{"aa":"v1"},{"bb":"v2"}],"a_size":2 }`

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [November 25, 2015, 7:12pm UTC](https://discuss.elastic.co/t/how-to-use-ruby-filter/35555/2 "2015-11-25T19:12:05Z")

</div>

```auto
filter {
  ruby {
    code => "event['a_size'] = event['a'].length"
  }
}

```

(You should wrap the filter in a conditional so you don't attempt this if the `a` field doesn't exist.)

---

<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: [July 6, 2017, 5:21am UTC](https://discuss.elastic.co/t/how-to-use-ruby-filter/35555/3 "2017-07-06T05:21:12Z")

</div>


