# How can I create new field inside an array of obejcts?

**URL:** <https://discuss.elastic.co/t/how-can-i-create-new-field-inside-an-array-of-obejcts/308036>\
**Category:** Logstash\
**Created:** [June 23, 2022, 3:54pm UTC](https://discuss.elastic.co/t/how-can-i-create-new-field-inside-an-array-of-obejcts/308036 "2022-06-23T15:54:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jvinacio](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jvinacio](https://discuss.elastic.co/u/jvinacio)\
**Post date:** [June 23, 2022, 3:54pm UTC](https://discuss.elastic.co/t/how-can-i-create-new-field-inside-an-array-of-obejcts/308036/1 "2022-06-23T15:54:43Z")

</div>

Hello!  
I have the following array:  
{  
"tempoTotalDayEvents" : [  
{  
"tempo7" : 25.0  
},  
{  
"tempo8" : 0.0  
},  
{  
"tempo11" : 0.0  
},  
{  
"tempo12" : 6.0  
},  
{  
"tempo13" : 0.0  
},  
{  
"tempo14" : 3.0  
},  
{  
"tempo15" : 323.0  
},  
{  
"tempo16" : 0.0  
},  
{  
"tempo17" : 163.0  
},  
{  
"tempo18" : 1170.0  
}  
And I need to create a new field inside of each object with the value inside the key name and remove after, like the following:  
{  
"tempo" : 25.0  
"hora":07  
},  
{  
"tempo" : 0.0  
"hora":08  
},  
{  
"tempo" : 0.0  
"hora":11  
},  
{  
"tempo" : 6.0  
"hora":12  
},  
{  
"tempo" : 0.0  
"hora":13  
},  
{  
"tempo" : 3.0  
"hora":14  
},  
{  
"tempo" : 323.0  
"hora":15  
},  
{  
"tempo" : 0.0  
"hora":16  
},  
{  
"tempo" : 163.0  
"hora":17  
},  
{  
"tempo" : 1170.0  
"hora":18  
}  
Is it possible?

---

<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:** [June 23, 2022, 5:11pm UTC](https://discuss.elastic.co/t/how-can-i-create-new-field-inside-an-array-of-obejcts/308036/2 "2022-06-23T17:11:27Z")

</div>

You could start with

```
    ruby {
        code => '
            t = event.get("tempoTotalDayEvents")
            if t.is_a? Array
                t.each_index { |x|
                    matches = t[x].keys[0].scan(/^tempo(\d+)$/)
                    t[x]["hora"] = matches[0][0].to_i
                }
                event.set("tempoTotalDayEvents", t)
            end
        '
    }

```

which produces

```
"tempoTotalDayEvents" => [
    [0] {
          "hora" => 7,
        "tempo7" => 25.0
    },
    [1] {
          "hora" => 8,
        "tempo8" => 0.0
    },
    [2] {
           "hora" => 11,
        "tempo11" => 0.0
    },

```

which is not quite what you asked for, but what you asked for is not valid.

Note that the call to t[x].keys[0].scan makes all kinds of assumptions about the structure of the entries in [tempoTotalDayEvents]. If those assumptions turn out to be invalid then it will result in exceptions. Ideally you should add code to test those assumption, or else to catch the exceptions.

---

<div class="post-metadata">

**Author:** ![jvinacio](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@jvinacio](https://discuss.elastic.co/u/jvinacio)\
**Post date:** [June 23, 2022, 5:42pm UTC](https://discuss.elastic.co/t/how-can-i-create-new-field-inside-an-array-of-obejcts/308036/3 "2022-06-23T17:42:28Z")

</div>

Thank you very much again! You saved my ass 2 times in a weak!  
Thank you!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 21, 2022, 5:42pm UTC](https://discuss.elastic.co/t/how-can-i-create-new-field-inside-an-array-of-obejcts/308036/4 "2022-07-21T17:42:55Z")

</div>

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