# Help parsing nested JSON with dynamic top key

**URL:** <https://discuss.elastic.co/t/help-parsing-nested-json-with-dynamic-top-key/41069>\
**Category:** Logstash\
**Created:** [February 5, 2016, 1:41pm UTC](https://discuss.elastic.co/t/help-parsing-nested-json-with-dynamic-top-key/41069 "2016-02-05T13:41:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thesilence](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@thesilence](https://discuss.elastic.co/u/thesilence)\
**Post date:** [February 5, 2016, 1:41pm UTC](https://discuss.elastic.co/t/help-parsing-nested-json-with-dynamic-top-key/41069/1 "2016-02-05T13:41:17Z")

</div>

Hi everyone,

I am looking for help to tackle a fairly difficult problem for an unexperienced logstash user.

I have an application that pushes JSON messages to redis.

The message format is the following:  
[http://pastebin.com/gRyRAejb](http://pastebin.com/gRyRAejb)

I need to insert the contents of each message in ElasticSearch, but I want to get rid of the top level key (in the example called 'DYNAMIC\_TOP\_KEY', so I only want the values inside.

I don't really know how to perform this operation in logstash filters. I also cannot access the fields below as I don't know how to capture the top level key.

For example if I want to delete the "msg\_name", how should I do that?

Thanks a lot for the help in advance!

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [February 6, 2016, 2:50pm UTC](https://discuss.elastic.co/t/help-parsing-nested-json-with-dynamic-top-key/41069/2 "2016-02-06T14:50:19Z")

</div>

You'll have to use the ruby filter to copy key/value pairs of nested fields into the top level. This appears to work:

```auto
filter {
  ruby {
    code => "
      event.to_hash.clone.each_value{|v|
        if v.is_a? Hash
          v.each_pair{|k,v|
            event[k] = v
          }
        end
      }
    "
  }
}

```

---

<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:** [July 6, 2017, 5:12am UTC](https://discuss.elastic.co/t/help-parsing-nested-json-with-dynamic-top-key/41069/3 "2017-07-06T05:12:50Z")

</div>


