# How to take selected fields from nested array

**URL:** <https://discuss.elastic.co/t/how-to-take-selected-fields-from-nested-array/213188>\
**Category:** Logstash\
**Created:** [December 27, 2019, 12:26pm UTC](https://discuss.elastic.co/t/how-to-take-selected-fields-from-nested-array/213188 "2019-12-27T12:26:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![namali](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@namali](https://discuss.elastic.co/u/namali)\
**Post date:** [December 27, 2019, 12:26pm UTC](https://discuss.elastic.co/t/how-to-take-selected-fields-from-nested-array/213188/1 "2019-12-27T12:26:16Z")

</div>

sample json

data {   
Name: "Cesar Barroso"  
ageBands : [   
{   
ageFrom :25,  
ageTo :64,  
adult :true,  
treatAsAdult :true,  
description :"Adult"},  
{   
ageFrom :35,  
ageTo :25,  
adult :true,  
treatAsAdult :true,  
description :"Adult"},  
{..}  
]  
..  
}

ageBand is dynamic array. from that I wanted to get selected fields as  
ageBands : [  
{ageFrom :25,  
ageTo :64,  
description :"Adult"},  
{ageFrom :35,  
ageTo :25,  
description :"Adult"}  
]

I tried from ruby filter. unable to put loop

ruby {  
code =\> "  
event.set('ageBand',Hash['ageTo',event.get('[data][ageBands][1][ageTo]').to\_i,'ageFrom',event.get('[data][ageBands][1][ageFrom]').to\_i,'description', event.get('[data][ageBands][1][description]').to\_s])   
"}

---

<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:** [December 27, 2019, 3:41pm UTC](https://discuss.elastic.co/t/how-to-take-selected-fields-from-nested-array/213188/2 "2019-12-27T15:41:41Z")

</div>

You could try this

```
    ruby {
        code => '
            data = event.get("data")
            if data
                ageBands = []
                data["ageBands"].each { |x|
                    h = {}
                    h["ageFrom"] = x["ageFrom"]
                    h["ageTo"] = x["ageTo"]
                    h["description"] = x["description"]
                    ageBands << h
                }
                event.set("ageBands", ageBands)
            end
        '
    }
```

---

<div class="post-metadata">

**Author:** ![namali](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@namali](https://discuss.elastic.co/u/namali)\
**Post date:** [December 30, 2019, 5:27am UTC](https://discuss.elastic.co/t/how-to-take-selected-fields-from-nested-array/213188/3 "2019-12-30T05:27:11Z")

</div>

This is working, Thx Badger

---

<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:** [January 27, 2020, 5:27am UTC](https://discuss.elastic.co/t/how-to-take-selected-fields-from-nested-array/213188/4 "2020-01-27T05:27:14Z")

</div>

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