class LogStash::Codecs::JSON

This codec may be used to decode (via inputs) and encode (via outputs) full JSON messages. If the data being sent is a JSON array at its root multiple events will be created (one per element).

If you are streaming JSON messages delimited by 'n' then see the `json_lines` codec.

Encoding will result in a compact JSON representation (no line terminators or indentation)

If this codec recieves a payload from an input that is not valid JSON, then it will fall back to plain text and add a tag `_jsonparsefailure`. Upon a JSON failure, the payload will be stored in the `message` field.

Public Class Methods

new(*params) click to toggle source
Calls superclass method
# File lib/logstash/codecs/json.rb, line 51
def initialize(*params)
  super

  @original_field = ecs_select[disabled: nil, v1: '[event][original]']

  @converter = LogStash::Util::Charset.new(@charset)
  @converter.logger = @logger
end

Public Instance Methods

decode(data, &block) click to toggle source
# File lib/logstash/codecs/json.rb, line 64
def decode(data, &block)
  parse_json(@converter.convert(data), &block)
end
encode(event) click to toggle source
# File lib/logstash/codecs/json.rb, line 68
def encode(event)
  @on_event.call(event, event.to_json)
end
register() click to toggle source
# File lib/logstash/codecs/json.rb, line 60
def register
  # no-op
end

Private Instance Methods

parse_json(json) { |event| ... } click to toggle source
# File lib/logstash/codecs/json.rb, line 74
def parse_json(json)
  events = events_from_json(json, targeted_event_factory)
  if events.size == 1
    event = events.first
    event.set(@original_field, json.dup.freeze) if @original_field
    yield event
  else
    events.each { |event| yield event }
  end
rescue => e
  @logger.error("JSON parse error, original data now in message field", message: e.message, exception: e.class, data: json)
  yield parse_json_error_event(json)
end
parse_json_error_event(json) click to toggle source
# File lib/logstash/codecs/json.rb, line 88
def parse_json_error_event(json)
  event_factory.new_event("message" => json, "tags" => ["_jsonparsefailure"])
end