# How to move all the json fields at root level to a sub json node?

**URL:** <https://discuss.elastic.co/t/how-to-move-all-the-json-fields-at-root-level-to-a-sub-json-node/215522>\
**Category:** Logstash\
**Created:** [January 17, 2020, 8:34pm UTC](https://discuss.elastic.co/t/how-to-move-all-the-json-fields-at-root-level-to-a-sub-json-node/215522 "2020-01-17T20:34:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![STAR\_DEVX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/star_devx/32/61000_2.png) [@STAR\_DEVX](https://discuss.elastic.co/u/STAR_DEVX)\
**Post date:** [January 17, 2020, 8:34pm UTC](https://discuss.elastic.co/t/how-to-move-all-the-json-fields-at-root-level-to-a-sub-json-node/215522/1 "2020-01-17T20:34:50Z")

</div>

I want to move all the json node at the root level to a sub level jason node. What's the most efficient way to use filters to achieve that?

root level  
{  
"a": 1,  
"b": "str",  
"c": {"key": "value"}  
}

I want the result to be:  
{  
"data": {  
"a": 1,  
"b": "str",  
"c": {"key": "value"}  
}  
}

The most intuitive thought is to use mutate-rename filter.  
mutate {  
rename =\> { reference to the root level json =\> "data" }  
}  
However, I couldn't find a way to reference the root level json.

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:** [January 17, 2020, 10:49pm UTC](https://discuss.elastic.co/t/how-to-move-all-the-json-fields-at-root-level-to-a-sub-json-node/215522/2 "2020-01-17T22:49:59Z")

</div>

I would do something like this:

```
ruby {
    code => '
        event.to_hash.each { |k, v|
            unless ["@timestamp", "@version", "host", "message"].include?(k)
                event.set("[data][#{k}]", v)
                event.remove(k)
            end
        }
    '
}
```

---

<div class="post-metadata">

**Author:** ![STAR\_DEVX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/star_devx/32/61000_2.png) [@STAR\_DEVX](https://discuss.elastic.co/u/STAR_DEVX)\
**Post date:** [January 20, 2020, 9:55pm UTC](https://discuss.elastic.co/t/how-to-move-all-the-json-fields-at-root-level-to-a-sub-json-node/215522/3 "2020-01-20T21:55:32Z")

</div>

It 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:** [February 17, 2020, 9:55pm UTC](https://discuss.elastic.co/t/how-to-move-all-the-json-fields-at-root-level-to-a-sub-json-node/215522/4 "2020-02-17T21:55:36Z")

</div>

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