title: Loop through nested json in logstash with ruby
body: <p>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 <em>#i</em> ) but it only throws a _rubyexception:</p>
  
  <pre><code>ruby { code =&gt; '
   i = 0
   sum = 0
   while i &lt; event.get( "[foo][bar][faz]" ).length do
    sum += event.get( "[foo][bar][faz][#i][[baz]]" )
    i += 1
   end
   event.set( "sum", sum )
  '
  }
  </code></pre>
  
  <p>How should I properly loop through the nested fields to get the sum?</p>
  
  <p>e.g. This works:</p>
  
  <pre><code>ruby { code =&gt; '
    event.set( "sum", event.get( "[foo][bar][faz][0][[baz]]" ) + event.get( "[foo][bar][faz][1][[baz]]" ) )
   '
  }
  </code></pre>
  
  <p>but the real amount of events vary.</p>