Create field with condition while looping through an array in ruby

Hello,

I use logstash 7.17.

I want to create a field while looping through an array in ruby from my data. Here is a snippet of my code which doesn't work :

items_contact = event.get("[contactMedium]")
    if items_contact != nil
        items_contact.each_with_index { |i, x|
            if (i["mediumType"] == "address") then
                if i["characteristic"]["street"] then
                    event.set("[user][address]", i["characteristic"]["street"])
                end
                if i["characteristic"]["city"] then
                    event.set("[user][city]", i["characteristic"]["city"])
                end
            elsif (i["mediumType"] == "contact") then
                if i["characteristic"]["contactNumber"] then
                    event.set("[user][contactNumber]", i["characteristic"]["contactNumber"])
                end
            elsif (i["mediumType"] == "email") then
                if i["characteristic"]["email"] then
                    event.set("[user][email]", i["characteristic"]["email"])
                end
            end
        }
    end

Here is my input in json :

{
  "contactMedium": [
    { "mediumType": "address", "characteristic": { "city": "Paris", "street": "2 rue bidule" } },
    { "mediumType": "email", "characteristic": { "email": "test@test.com" } },
    { "mediumType": "contact", "characteristic": { "contactNumber": "0600000000"} }
  ]
}

Here is what I want:

"user": {
      "address": "2 rue bidule",
      "city": "France",
      "contactNumber": "0600000000",
      "email": "test@test.com"
}

How can I create these fields while in a loop ?

Any help would be appreciated, thank you in advance

Just the way you tried, but you need to fetch the right field from the event.

items_contact = event.get("[contactMedium]")

will produce

         "user" => {
            "email" => "test@test.com",
          "address" => "2 rue bidule",
             "city" => "Paris",
    "contactNumber" => "0600000000"
}

And what if I want to get the result in a nested with something like that in the output :

"user": {
      "other" : {
            "address": "2 rue bidule",
            "city": "France",
            "contactNumber": "0600000000",
            "email": "test@test.com"
      }
}

How can I do it ?
If I try to add it in code like following I don't get my result

items_contact = event.get("[contact]")
    if items_contact != nil
        items_contact.each_with_index { |i, x|
            if (i["mediumType"] == "address") then
                if i["characteristic"]["street"] then
                    event.set("[user][other][address]", i["characteristic"]["street"])
                end
                if i["characteristic"]["city"] then
                    event.set("[user][other][city]", i["characteristic"]["city"])
                end
            elsif (i["mediumType"] == "contact") then
                if i["characteristic"]["contactNumber"] then
                    event.set("[user][other][contactNumber]", i["characteristic"]["contactNumber"])
                end
            elsif (i["mediumType"] == "email") then
                if i["characteristic"]["email"] then
                    event.set("[user][other][email]", i["characteristic"]["email"])
                end
            end
        }
    end

I do not know. Provided I use items_contact = event.get("[contactMedium]") it works as expected for me.

I had a problem with something else, thank you