# Parsing nested JSON with dynamic key

**URL:** <https://discuss.elastic.co/t/parsing-nested-json-with-dynamic-key/173110>\
**Category:** Logstash\
**Created:** [March 20, 2019, 10:08am UTC](https://discuss.elastic.co/t/parsing-nested-json-with-dynamic-key/173110 "2019-03-20T10:08:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdobrin](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mdobrin](https://discuss.elastic.co/u/mdobrin)\
**Post date:** [March 20, 2019, 10:08am UTC](https://discuss.elastic.co/t/parsing-nested-json-with-dynamic-key/173110/1 "2019-03-20T10:08:35Z")

</div>

Given the following JSON, I would like to be able to extract the genreId.  
However, I am not sure how to do so with nested dynamic keys (in the example below, "610015")

> {  
> "b\_shopId": 502192,  
> "body": {  
> "delivery": {  
> "deliveryName": "My Delivery"  
> },  
> "item": {  
> "610015": {  
> "genreId": 216461  
> }  
> },  
> "orderNumber": "502192-20190320-00066801",  
> "statusId": 1  
> }  
> }

My filter currently is defined as follows, but obviously fails due to the name "aDynamicKey":

> ```
> filter {
> 
> json {
> source => "message"
> # target => "message_target"
> }
> split {
> field => "[body]"
> }
> 
> mutate {
> add_field => {
> "b_shopId" => "%{[body][shop][shopId]}"
> "b_genreId" => "%{[body][aDynamicKey][genreId]}"
> }
> }
> mutate {
> convert => {
> "b_shopId" => "integer"
> }
> 
> remove_field => ["[message]" ]
> }
> 
> ```
> 
> }

---

<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 20, 2019, 1:12pm UTC](https://discuss.elastic.co/t/parsing-nested-json-with-dynamic-key/173110/2 "2019-03-20T13:12:52Z")

</div>

You could do that with ruby

```
    ruby {
        code => '
            h = event.get("[body][item]")
            h.each { |k, v|
                v.each { |l, w|
                    event.set(l, w)
                }
            }
        '
    }
```

---

<div class="post-metadata">

**Author:** ![mdobrin](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mdobrin](https://discuss.elastic.co/u/mdobrin)\
**Post date:** [March 22, 2019, 2:11am UTC](https://discuss.elastic.co/t/parsing-nested-json-with-dynamic-key/173110/3 "2019-03-22T02:11:55Z")

</div>

Thanks that did the trick!

---

<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 19, 2019, 2:12am UTC](https://discuss.elastic.co/t/parsing-nested-json-with-dynamic-key/173110/4 "2019-04-19T02:12:00Z")

</div>

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