How to parse json in logstash /grok from a text file line?
Asked Answered
L

1

12

I have a logfile which looks like this ( simplified)

Logline sample

MyLine data={"firstname":"bob","lastname":"the builder"}

I'd like to extract the json contained in data and create two fields, one for firstname, one for last. However, the ouput i get is this:

{"message":"Line data={\"firstname\":\"bob\",\"lastname\":\"the builder\"}\r","@version":"1","@timestamp":"2015-11-26T11:38:56.700Z","host":"xxx","path":"C:/logstashold/bin/input.txt","MyWord":"Line","parsedJson":{"firstname":"bob","lastname":"the builder"}}

As you can see

..."parsedJson":{"firstname":"bob","lastname":"the builder"}}

That's not what I need, I need to create fields for firstname and lastname in kibana, but logstash isn't extracting the fields out with the json filter.

LogStash Config

input {
  file {
        path => "C:/logstashold/bin/input.txt"        
       }
}

filter {     

   grok {
            match => { "message" => "%{WORD:MyWord} data=%{GREEDYDATA:request}"}        
        }   

    json{
        source => "request"
        target => "parsedJson"
        remove_field=>["request"]
    }   
}   

output {  
    file{
        path => "C:/logstashold/bin/output.txt"
    }   
}

Any help greatly appreciated, I'm sure I'm missing out something simple

Thanks

Literator answered 26/11, 2015 at 11:49 Comment(0)
S
22

After your json filter add another one called mutate in order to add the two fields that you would take from the parsedJson field.

filter {
  ...
  json {
     ...
  }
  mutate {
    add_field => {
      "firstname" => "%{[parsedJson][firstname]}"
      "lastname" => "%{[parsedJson][lastname]}"
    }
  }
}

For your sample log line above that would give:

{
       "message" => "MyLine data={\"firstname\":\"bob\",\"lastname\":\"the builder\"}",
      "@version" => "1",
    "@timestamp" => "2015-11-26T11:54:52.556Z",
          "host" => "iMac.local",
        "MyWord" => "MyLine",
    "parsedJson" => {
        "firstname" => "bob",
         "lastname" => "the builder"
    },
     "firstname" => "bob",
      "lastname" => "the builder"
}
Seersucker answered 26/11, 2015 at 11:54 Comment(2)
Your example has a redundant '}' after "firstname" and last name => %{[parsedJson][firstname]}}" which will prevent it from working properly.Mcqueen
Thanks @Mcqueen fixed ;-)Seersucker

© 2022 - 2024 — McMap. All rights reserved.