# Move all fields to a subfield?

**URL:** <https://discuss.elastic.co/t/move-all-fields-to-a-subfield/225563>\
**Category:** Logstash\
**Created:** [March 29, 2020, 2:51pm UTC](https://discuss.elastic.co/t/move-all-fields-to-a-subfield/225563 "2020-03-29T14:51:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sliddjur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sliddjur/32/65260_2.png) [@sliddjur](https://discuss.elastic.co/u/sliddjur)\
**Post date:** [March 29, 2020, 2:51pm UTC](https://discuss.elastic.co/t/move-all-fields-to-a-subfield/225563/1 "2020-03-29T14:51:53Z")

</div>

I want to move all my fields to a specified subfield. However, I don't know all the field names.

Alternatively if there is a way for grok filter to set the `target` field or similar?

I found an old post that did this, which crashed my logstash (v7.6)

```auto
    ruby {
        code => "
            event['new_val'].each {|k,v|
                event[k] = v
                }
            }
            event.remove('new_val')
        "
    }

```

---

<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:** [March 29, 2020, 3:31pm UTC](https://discuss.elastic.co/t/move-all-fields-to-a-subfield/225563/2 "2020-03-29T15:31:32Z")

</div>

Referring to the event as a hash was disabled years ago, you need to use the [event API](https://www.elastic.co/guide/en/logstash/current/event-api.html), like [this](https://discuss.elastic.co/t/how-to-dynamically-move-nested-key-value-to-root-level/180006/2).

---

<div class="post-metadata">

**Author:** ![sliddjur](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sliddjur/32/65260_2.png) [@sliddjur](https://discuss.elastic.co/u/sliddjur)\
**Post date:** [March 30, 2020, 10:29am UTC](https://discuss.elastic.co/t/move-all-fields-to-a-subfield/225563/3 "2020-03-30T10:29:00Z")

</div>

For future reference, this was what I was looking for:

```auto
    ruby {
        code => '
            event.to_hash.each { |k, v|
                event.set("[field][" . k . "]" , v)
            }
        '
    }

```

But what was even better, is that I found out that the new grok-filter version `4.3.0` does indeed come with a `target` option, which is essentially what I actually wanted.

---

<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:** [April 27, 2020, 10:29am UTC](https://discuss.elastic.co/t/move-all-fields-to-a-subfield/225563/4 "2020-04-27T10:29:02Z")

</div>

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