# Flattening array and dynamically create the field name

**URL:** <https://discuss.elastic.co/t/flattening-array-and-dynamically-create-the-field-name/229268>\
**Category:** Logstash\
**Created:** [April 22, 2020, 1:03pm UTC](https://discuss.elastic.co/t/flattening-array-and-dynamically-create-the-field-name/229268 "2020-04-22T13:03:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manto](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@manto](https://discuss.elastic.co/u/manto)\
**Post date:** [April 22, 2020, 1:03pm UTC](https://discuss.elastic.co/t/flattening-array-and-dynamically-create-the-field-name/229268/1 "2020-04-22T13:03:28Z")

</div>

Hello,

I have an JSON array input below:

```auto
"coverageStats": [
                {
                    "label": "Branches",
                    "position": 6,
                    "total": 30,
                    "covered": 17,
                    "isDeltaAvailable": false,
                    "delta": 0.0
                },
                {
                    "label": "Lines",
                    "position": 4,
                    "total": 492,
                    "covered": 117,
                    "isDeltaAvailable": false,
                    "delta": 0.0
                }
            ]

```

and I would like to create something like this with a ruby filter:

Desired output:

```auto
 "Branches" : {
                    "label": "Branches"
                    "position": 6,
                    "total": 30,
                    "covered": 17,
                    "isDeltaAvailable": false,
                    "delta": 0.0
},
"Lines" : {
                   "label": "Lines",
                    "position": 4,
                    "total": 492,
                    "covered": 117,
                    "isDeltaAvailable": false,
                    "delta": 0.0
}

```

Basically, I would like to iterate the array and take the "label" value and set it as a key in my new structured dictionary. By doing this, it would make easier to display the event in Kibana.

Any help would be appreciated.  
Thank you.

---

<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:** [April 22, 2020, 3:31pm UTC](https://discuss.elastic.co/t/flattening-array-and-dynamically-create-the-field-name/229268/2 "2020-04-22T15:31:12Z")

</div>

You could do that using ruby. I haven't tested it, but I would start with something like

```
ruby {
    code => '
        event.get("coverageStats").each { |v|
            event.set(v["label"], v)
        }
    '
}
```

---

<div class="post-metadata">

**Author:** ![manto](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@manto](https://discuss.elastic.co/u/manto)\
**Post date:** [April 23, 2020, 9:42am UTC](https://discuss.elastic.co/t/flattening-array-and-dynamically-create-the-field-name/229268/3 "2020-04-23T09:42:17Z")

</div>

Works like a charm!

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:** [May 21, 2020, 9:44am UTC](https://discuss.elastic.co/t/flattening-array-and-dynamically-create-the-field-name/229268/4 "2020-05-21T09:44:16Z")

</div>

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