[英]Accessing nested JSON Value with variable key name in Logstash
I've got a question regarding JSON in Logstash. 我在Logstash中有一个关于JSON的问题。 I have got a JSON Input that looks something like this: 我有一个看起来像这样的JSON输入:
{
"2": {
"name": "name2",
"state": "state2"
},
"1": {
"name": "name1",
"state": "state1"
},
"0": {
"name": "name0",
"state": "state0"
}
}
Now, let's say I want to add a field in the logstash config 现在,假设我想在logstash配置中添加一个字段
json{
source => "message"
add_field => {
"NAME" => "%{ What to write here ?}"
"STATE" => "%{ What to write here ?}"
}
}
Is there a way to access the JSON Input such that I get a field Name with value name1, another field with name 2 and a third field with name 3. The first key in the JSON is changing, that means there can only be one or many more parts. 有没有办法访问JSON输入,以便我得到一个值为name1的字段名称,另一个名称为2的字段和一个名称为3的第三个字段.JSON中的第一个键是更改,这意味着只能有一个或更多的部分。 So I don't want to hardcode it like 所以我不想像它那样硬编码
%{[0][name]}
Thanks for your help. 谢谢你的帮助。
If you remove all new lines in your input you can simply use the json filter . 如果删除输入中的所有新行,则只需使用json过滤器即可 。 You don't need any add_field
action. 您不需要任何add_field
操作。
Working config without new lines: 没有新行的工作配置:
filter {
json { source => message }
}
If you can't remove the new lines in your input you need to merge the lines with the multiline codec . 如果无法删除输入中的新行,则需要将这些行与多行编解码器合并。
Working config with new lines: 使用新行进行工作配置:
input {
file {
path => ["/path/to/your/file"] # I suppose your input is a file.
start_position => "beginning"
sincedb_path => "/dev/null" # just for testing
codec => multiline {
pattern => "^}"
what => "previous"
negate => "true"
}
}
}
filter {
mutate { replace => { "message" => "%{message}}" } }
json { source => message }
}
I suppose that you use the file input. 我想你使用文件输入。 In case you don't, just change it. 如果你不这样做,只需改变它。
Output (for both): 输出(两者):
"2" => {
"name" => "name2",
"state" => "state2"
},
"1" => {
"name" => "name1",
"state" => "state1"
},
"0" => {
"name" => "name0",
"state" => "state0"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.