class LogStash::Filters::JSONEncode

JSON encode filter. Takes a field and serializes it into JSON

If no target is specified, the source field is overwritten with the JSON text.

For example, if you have a field named 'foo', and you want to store the JSON encoded string in 'bar', do this:

filter {
  json_encode {
    source => "foo"
    target => "bar"
  }
}

Public Instance Methods

filter(event) click to toggle source
# File lib/logstash/filters/json_encode.rb, line 37
def filter(event)
  return unless filter?(event)

  @logger.debug("Running JSON encoder", :event => event)

  begin
    event[@target] = JSON.pretty_generate(event[@source])
    filter_matched(event)
  rescue => e
    event.tag "_jsongeneratefailure"
    @logger.warn("Trouble encoding JSON", :source => @source, :raw => event[@source].inspect, :exception => e)
  end

  @logger.debug? && @logger.debug("Event after JSON encoder", :event => event)
end
register() click to toggle source
# File lib/logstash/filters/json_encode.rb, line 32
def register
  @target = @source if @target.nil?
end