How to navigate with event,get method of Event API


(Ritesh) #1

Hi

I have a json file , for which i am not able to navigate to the events with logstash 5.0.

I want to extract the name of cars from below
using get and set method of ruby as per the documentation
event.get('[message][cars[name]')

{
    "name":"John",
    "age":30,
    "cars": [
        { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
        { "name":"BMW", "models":[ "320", "X3", "X5" ] },
        { "name":"Fiat", "models":[ "500", "Panda" ] }
    ]
 }

Here is my conf file

input {  
      file {
	  
          sincedb_path =>  "..../bin/sincedb_path.txt"
          path => "..../test1.json"
          type => "test"
          start_position => "beginning"
		  
      }
}


    
filter {

ruby {
    code => "
test = event.get('[message][cars][name]')
event.set('result',test)
	"
	}

}

output {  
stdout { codec => rubydebug}
 
 }

With the above config file i get nil value.
Please let me know correct way to navigate to the path


(Magnus B├Ąck) #2

cars or [cars] addresses the "cars" array itself and [cars][0][name] addresses the "name" field of the first item in the array.


(system) #3

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