sum all fields in nested json logstash

JavaScript
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 ) 
 '
}
Source

Also in JavaScript: