Skip to content

Commit 5e3553e

Browse files
committed
Support dynamic target field names.
It's conceivable that one would want the target field name to be chosen dynamically using the contents of another field, so pass the target option value through sprintf.
1 parent 2c1e8cb commit 5e3553e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/logstash/filters/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def filter(event)
8383
end
8484

8585
if @target
86-
event.set(@target, parsed)
86+
event.set(event.sprintf(@target), parsed)
8787
else
8888
unless parsed.is_a?(Hash)
8989
@tag_on_failure.each{|tag| event.tag(tag)}

spec/filters/json_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@
4141
end
4242
end
4343

44+
describe "parse message into a dynamically named target field" do
45+
config <<-CONFIG
46+
filter {
47+
json {
48+
# Parse message as JSON, store the results in the 'data' field'
49+
source => "message"
50+
target => "%{target}"
51+
}
52+
}
53+
CONFIG
54+
55+
sample({"target" => "data", "message" => '{ "hello": "world" }'}) do
56+
insist { subject.get("data")["hello"] } == "world"
57+
end
58+
end
59+
4460
describe "tag invalid json" do
4561
config <<-CONFIG
4662
filter {

0 commit comments

Comments
 (0)