# Length of an array in logstash

**URL:** https://discuss.elastic.co/t/length-of-an-array-in-logstash/61750
**Category:** Logstash
**Created:** [September 28, 2016, 10:45pm UTC](https://discuss.elastic.co/t/length-of-an-array-in-logstash/61750 "2016-09-28T22:45:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![saisn](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@saisn](https://discuss.elastic.co/u/saisn)
#### Post date: [September 28, 2016, 10:45pm UTC](https://discuss.elastic.co/t/length-of-an-array-in-logstash/61750/1 "2016-09-28T22:45:54Z")

</div>

I am trying to find length of an array for a field using ruby filter. But, I'm getting below error even though nul case is handled. Please let me know if there is any error in the filter.  
Thanks in advance

filter {  
ruby {  
code =\> "event['failedattemptcount'] = event['failedattempttimes'].length if event['failedattempttimes'] != 'nil'"  
}  
}

# error:

Ruby exception occurred: undefined method `length' for nil:NilClass {:level=\>:eror}

"tags" =\> [  
[0] "\_rubyexception"  
]

---

<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: [September 29, 2016, 5:38am UTC](https://discuss.elastic.co/t/length-of-an-array-in-logstash/61750/2 "2016-09-29T05:38:08Z")

</div>

You're checking if the `failedattempttimes` field is equal to the string "nil". Do this instead:

```
code => "event['failedattemptcount'] = event['failedattempttimes'].length unless event['failedattempttimes'].nil?"
```

---

<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, 4:36am UTC](https://discuss.elastic.co/t/length-of-an-array-in-logstash/61750/3 "2017-07-06T04:36:30Z")

</div>


