File tree 2 files changed +24
-1
lines changed
2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,12 @@ class LogStash::Filters::Json < LogStash::Filters::Base
52
52
# JSON in the value of the `source` field will be expanded into a
53
53
# data structure in the `target` field.
54
54
#
55
+ # The string in the `target` option may contain dynamic sprintf-style
56
+ # field references, i.e. the contents of one field can be used to choose
57
+ # the name of the target field. Dynamic field references can only refer
58
+ # to already existing fields, so the JSON string being parsed can't
59
+ # contain the name of the target field.
60
+ #
55
61
# NOTE: if the `target` field already exists, it will be overwritten!
56
62
config :target , :validate => :string
57
63
@@ -83,7 +89,7 @@ def filter(event)
83
89
end
84
90
85
91
if @target
86
- event . set ( @target , parsed )
92
+ event . set ( event . sprintf ( @target ) , parsed )
87
93
else
88
94
unless parsed . is_a? ( Hash )
89
95
@tag_on_failure . each { |tag | event . tag ( tag ) }
Original file line number Diff line number Diff line change 41
41
end
42
42
end
43
43
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 field named
49
+ # by the contents of the 'target' field'
50
+ source => "message"
51
+ target => "%{target}"
52
+ }
53
+ }
54
+ CONFIG
55
+
56
+ sample ( { "target" => "data" , "message" => '{ "hello": "world" }' } ) do
57
+ insist { subject . get ( "[data][hello]" ) } == "world"
58
+ end
59
+ end
60
+
44
61
describe "tag invalid json" do
45
62
config <<-CONFIG
46
63
filter {
You can’t perform that action at this time.
0 commit comments