Reputation: 89
My data is deeply nested JSON, and I need to index a sum of certain fields. As new to Ruby, I've tried the following (with some variations of #i ) but it only throws a _rubyexception:
ruby { code => '
i = 0
sum = 0
while i < event.get( "[foo][bar][faz]" ).length do
sum += event.get( "[foo][bar][faz][#i][[baz]]" )
i += 1
end
event.set( "sum", sum )
'
}
How should I properly loop through the nested fields to get the sum?
e.g. This works:
ruby { code => '
event.set( "sum", event.get( "[foo][bar][faz][0][[baz]]" ) + event.get( "[foo][bar][faz][1][[baz]]" ) )
'
}
but the real amount of events vary.
Upvotes: 0
Views: 2695
Reputation: 89
I found a solution to my question:
ruby { code => '
i = 0
sum = 0
while i < event.get( "[foo][bar][faz]" ).length do
sum += event.get( "[foo][bar][faz][" + i.to_s + "][[baz]]" )
i += 1
end
event.set( "sum", sum )
'
}
Upvotes: 1