# Create field with condition while looping through an array in ruby

**URL:** <https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444>\
**Category:** Logstash\
**Created:** [July 19, 2024, 3:39pm UTC](https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444 "2024-07-19T15:39:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Armalyca](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Armalyca](https://discuss.elastic.co/u/Armalyca)\
**Post date:** [July 19, 2024, 3:39pm UTC](https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444/1 "2024-07-19T15:39:52Z")

</div>

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 :

```auto
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 :

```auto
{
  "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:

```auto
"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

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [July 19, 2024, 4:59pm UTC](https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444/2 "2024-07-19T16:59:34Z")

</div>

> [@Armalyca](#):
>
> How can I create these fields while in a loop ?

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"
}

```

---

<div class="post-metadata">

**Author:** ![Armalyca](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Armalyca](https://discuss.elastic.co/u/Armalyca)\
**Post date:** [July 24, 2024, 9:01am UTC](https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444/3 "2024-07-24T09:01:49Z")

</div>

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

```auto
"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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [July 24, 2024, 12:32pm UTC](https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444/4 "2024-07-24T12:32:09Z")

</div>

> [@Armalyca](#):
>
> How can I do it ?

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

---

<div class="post-metadata">

**Author:** ![Armalyca](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Armalyca](https://discuss.elastic.co/u/Armalyca)\
**Post date:** [July 25, 2024, 1:59pm UTC](https://discuss.elastic.co/t/create-field-with-condition-while-looping-through-an-array-in-ruby/363444/5 "2024-07-25T13:59:59Z")

</div>

I had a problem with something else, thank you
