# Flatten json and create dynamic field

**URL:** <https://discuss.elastic.co/t/flatten-json-and-create-dynamic-field/240360>\
**Category:** Logstash\
**Created:** [July 8, 2020, 1:57pm UTC](https://discuss.elastic.co/t/flatten-json-and-create-dynamic-field/240360 "2020-07-08T13:57:14Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Jenni](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jenni/32/29684_2.png) [@Jenni](https://discuss.elastic.co/u/Jenni)\
**Post date:** [July 8, 2020, 4:18pm UTC](https://discuss.elastic.co/t/flatten-json-and-create-dynamic-field/240360/9 "2020-07-08T16:18:31Z")

</div>

> [@elasticforme](#):
>
> used this code but didn't work
> 
> ```auto
> if [dataflow] {
> ruby {
> code => '
> kv = event.get("dataflow")
> kv.to_hash.each { |k,v|
> event.set(k, v)
> }
> '
> }
> }
> 
> ```

With "flatten out" you mean transfer those fields to the root level of your event, right? Then your code is close to the solution. But dataflow is an array with the actual data in the first entry. And you need to remove the field afterwards. I think it should be like this:

```auto
if [dataflow] {
   ruby {
     code => '
         event.get("[dataflow][0]").each { |k,v|
           event.set(k, v)
         }
         event.remove("dataflow")
   '
   }
}

```

---

_[View the full topic](https://discuss.elastic.co/t/flatten-json-and-create-dynamic-field/240360)._
