Logstash 5.0 Ruby Filter Can't Update Hash in Array

I'm a newbie to Logstash and Ruby, and I meet a subtle problem today.

My input JSON like the following:

{
  "1": "1",
  "2": "2",
  "market": [
    {
      "id": "1",
      "name": "m1"
    },
    {
      "id": "2",
      "name": "m2"
    }
  ]
}

My filter is:

filter {
    ......
    ruby {
        code => "
        markets = event.get('market')
        markets.each_index do |index| 
    event.set(markets[index]['id'], markets[index]['name']) 
    markets[index]['id'] = markets[index]['name']
    end
"
}
......
}

And the output is:

{
  "1": "m1",
  "2": "m2",
  "market": [
    {
      "id": "1",
      "name": "m1"
    },
    {
      "id": "2",
      "name": "m2"
    }
  ]
}

What I expect to be:

{
  "1": "m1",
  "2": "m2",
  "market": [
    {
      "id": "m1",
      "name": "m1"
    },
    {
      "id": "m2",
      "name": "m2"
    }
  ]
}

I think market.id should be m1 not 1, could anyone give me some clue what's wrong with my filter?
The logstash I'm using is version 5.0.

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