# Get field with nested array using ruby filter

**URL:** <https://discuss.elastic.co/t/get-field-with-nested-array-using-ruby-filter/100661>\
**Category:** Logstash\
**Created:** [September 15, 2017, 9:20am UTC](https://discuss.elastic.co/t/get-field-with-nested-array-using-ruby-filter/100661 "2017-09-15T09:20:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [September 15, 2017, 9:20am UTC](https://discuss.elastic.co/t/get-field-with-nested-array-using-ruby-filter/100661/1 "2017-09-15T09:20:59Z")

</div>

Hi All

I have a json data in this format

```
`{"foo":"bar","test":{"steps":[{"response_time":"100"},{"response_time":"101","more_nested":[{"hello":"world"},{"hello2":"world2"}]}]}}`

```

i am getting result in kibana as below after using json filter

```
foo: bar,
test.steps : { "response_time": "100" }, { "more_nested": [{ "hello": "world" }, { "hello2": "world2" }], "response_time": "101" }

```

My expected result is

```
foo: bar
test.steps.responsetime :100
test.steps.more_nested.hello : world
test.steps.more_nested.hell2 : world2

```

How can achieve this with ruby filter, i have tried this below but not working  
after reading multiple old thread i got this, but looks like there is some problem with 5.x version. Please me know the problem below

```
ruby {
    init => "
        def arrays_to_hash(h)
          h.each do |k,v|
           
            value = v || k
            if value.is_a?(Array)
              
                value_hash = {}
                value.each_with_index do |v, i|
                    value_hash[i.to_s] = v
                end
                h[k] = value_hash
            end

            if value.is_a?(Hash) || value.is_a?(Array)
              arrays_to_hash(value)
            end
          end
        end
      "
      code => "
	  
	  event.set('newevent',arrays_to_hash )
	  
	  "
}
```

---

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [September 16, 2017, 5:19am UTC](https://discuss.elastic.co/t/get-field-with-nested-array-using-ruby-filter/100661/2 "2017-09-16T05:19:34Z")

</div>

any comments to my code above, i am still not able to proceed.  
Thanks

---

<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:** [October 14, 2017, 5:19am UTC](https://discuss.elastic.co/t/get-field-with-nested-array-using-ruby-filter/100661/3 "2017-10-14T05:19:40Z")

</div>

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