Get field with nested array using ruby filter


(Ritesh) #1

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 )
	  
	  "
}

(Ritesh) #2

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


(system) #3

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