# Splitting fields into new documents

**URL:** <https://discuss.elastic.co/t/splitting-fields-into-new-documents/251459>\
**Category:** Logstash\
**Created:** [October 8, 2020, 2:13pm UTC](https://discuss.elastic.co/t/splitting-fields-into-new-documents/251459 "2020-10-08T14:13:05Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [October 8, 2020, 3:55pm UTC](https://discuss.elastic.co/t/splitting-fields-into-new-documents/251459/2 "2020-10-08T15:55:06Z")

</div>

You can do that using ruby

```
    json { source => "message" target => "[@metadata][json]" remove_field => ["message"] }
    ruby {
        code => '
            a = []
            a[0] = {}
            event.get("[@metadata][json]").each { |k, v|
                if k =~ /\w+\.\d+/
                    m = k.scan(/(\w+)\.(\d+)/)
                    a[m[0][1].to_i] ||= {}
                    a[m[0][1].to_i][m[0][0]] = v
                else
                    a[0][k] = v
                end
            }
            event.set("stuff", a)
        '
    }
    split { field => "stuff" }
```

---

_[View the full topic](https://discuss.elastic.co/t/splitting-fields-into-new-documents/251459)._
