# Using ruby filter to sum values of nested objects everytime the event matches

**URL:** <https://discuss.elastic.co/t/using-ruby-filter-to-sum-values-of-nested-objects-everytime-the-event-matches/220248>\
**Category:** Logstash\
**Created:** [February 20, 2020, 6:20pm UTC](https://discuss.elastic.co/t/using-ruby-filter-to-sum-values-of-nested-objects-everytime-the-event-matches/220248 "2020-02-20T18:20:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kassio\_Silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kassio_silva/32/62747_2.png) [@Kassio\_Silva](https://discuss.elastic.co/u/Kassio_Silva)\
**Post date:** [February 20, 2020, 6:20pm UTC](https://discuss.elastic.co/t/using-ruby-filter-to-sum-values-of-nested-objects-everytime-the-event-matches/220248/1 "2020-02-20T18:20:15Z")

</div>

Hello guys,

I trying to sum the value of a specific field everytime it shows:

the field is in this format: "[cdr][Service-Information][PS-Information][Service-Data-Container][Accounting-Output-Octets]" and it's value is a numeric field(it shows the number of bits consumed).

what i trying to do is the following(based on a reply i saw in this forum):

ruby {  
code =\> '  
a = event.get("[cdr][Service-Information][PS-Information][Service-Data-Container][Accounting-Output-Octets]")  
if a  
sum = 0  
a.each\_index { |x|  
sum += a["amount"]  
}  
event.set("amount-sum", sum)  
end  
'  
}

i getting the following error:

"Ruby exception occurred: undefined method `each\_index' for Integer"

i am a newbie in ruby, so i got no idea if this code serves for this type of field too.

Any help is appreciated! thanks in advanced! 🙂

---

<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:** [March 19, 2020, 6:20pm UTC](https://discuss.elastic.co/t/using-ruby-filter-to-sum-values-of-nested-objects-everytime-the-event-matches/220248/2 "2020-03-19T18:20:15Z")

</div>

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