Modifying an arrays value by index

I'm pretty new to logstash, and I need to replace a value in an array, using a kv filter's value,
but when I want to use the index variable nothing happens, if I use a simple integer instead, it works fine, here is my code I'm using:
"template" => [
[0] "Ön lekérdezte ",
[1] "id",
[2] " azonosítójú kártyája adatait"
ruby {code => "

            event.get('template').each_with_index do |item,index|
              hash = event.get('kv')
              hash.each { |key,value|
                if key.include? 'id'
                    event.set('[template][#{index}]', value)


Any ideas what I'm doing wrong?
Thank you

Hi there,

you using interpolation with single quoted string. You need double quotes to use it.

I'd refactor your code as following

filter {
  ruby {
    code => '
      hash = event.get("kv")
      event.get("template").each_with_index do |item, index|
        hash.each { |k, v| event.set("[template][#{index}]", v) if k.include? "id" }
1 Like

Thank you! :slight_smile:

No problem

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