从 JSON 中删除字符
Remove characters from JSON
我尝试用logstash解析一些json,目前我想输入的文件有以下结构(简化):
-4: {"audit":{"key1":"value1","key2":"value2"}}
-4: {"audit":{"key1":"value1","key2":"value2"}}
因此我需要删除 -4: 前缀以便使用 json 正确解析文件。不幸的是,我无法将 json 编解码器用于输入插件,因为它的 json 格式不正确。因此我对管道的要求是:
- 删除 -4: 前缀
- 将事件编码为 json
- 进行适当的突变
我试过以下管道,它给我一个解析错误:
input {
tcp {
port => 5001
codec => multiline {
pattern => "^-\d:."
what => previous
}
#codec => json_lines
type => "raw_input"
}
}
filter {
if [type] == "raw_input" {
mutate {
gsub => ["message", "^-\d:.", ""]
}
}
json {
source => "message"
}
mutate {
convert => { "[audit][sequenceNumber]" => "integer" }
add_field => { "test" => "%{[audit][sequenceNumber]}"}
}
}
output {
file {
path => "/var/log/logstash/debug-output.log"
codec => line { format => "%{message}" }
}
}
是否可以使用 logstash 实现此目的?有什么建议吗?
我会使用 dissect
filter
if [type] == "raw_input" {
dissect {
mapping => {
"message" => "-%{num}: %{msg}"
}
}
}
json {
source => "msg"
}
我尝试用logstash解析一些json,目前我想输入的文件有以下结构(简化):
-4: {"audit":{"key1":"value1","key2":"value2"}}
-4: {"audit":{"key1":"value1","key2":"value2"}}
因此我需要删除 -4: 前缀以便使用 json 正确解析文件。不幸的是,我无法将 json 编解码器用于输入插件,因为它的 json 格式不正确。因此我对管道的要求是:
- 删除 -4: 前缀
- 将事件编码为 json
- 进行适当的突变
我试过以下管道,它给我一个解析错误:
input {
tcp {
port => 5001
codec => multiline {
pattern => "^-\d:."
what => previous
}
#codec => json_lines
type => "raw_input"
}
}
filter {
if [type] == "raw_input" {
mutate {
gsub => ["message", "^-\d:.", ""]
}
}
json {
source => "message"
}
mutate {
convert => { "[audit][sequenceNumber]" => "integer" }
add_field => { "test" => "%{[audit][sequenceNumber]}"}
}
}
output {
file {
path => "/var/log/logstash/debug-output.log"
codec => line { format => "%{message}" }
}
}
是否可以使用 logstash 实现此目的?有什么建议吗?
我会使用 dissect
filter
if [type] == "raw_input" {
dissect {
mapping => {
"message" => "-%{num}: %{msg}"
}
}
}
json {
source => "msg"
}