如何在对象内部拆分JSON数组
我有一个数组中的数组的JSON消息。 我想把它分成多个事件:
{
"type": "monitor",
"server": "10.111.222.333",
"host": "abc.de",
"bean": [{
"name": "beanName1",
"reseted": "2015-06-05T15:10:00.192Z",
"method": [{
"name": "getAllXY",
"count": 5,
"min": 3,
"max": 5
},
{
"name": "getName",
"count": 4,
"min": 2,
"max": 4
}]
},
{
"name": "beanName2",
"reseted": "2015-06-05T15:10:00.231Z",
"method": [{
"name": "getProperty",
"count": 4,
"min": 3,
"max": 3
}]
},
{
"name": "beanName3",
"reseted": "2015-06-05T15:10:00.231Z"
}]
}
使用过滤器来分割“bean”:
input {
stdin {
codec => "json"
}
}
filter {
split {
field => "bean"
}
}
output {
stdout{codec => "json"}
}
运作良好:
{"type":"monitor",
"server":"10.111.222.333",
"host":"abc.de",
"bean":{
"name":"beanName1",
"reseted":"2015-06-05T15:10:00.192Z",
"method":[{
"name":"getAllXY",
"count":5,
"min":3,
"max":5
},{
"name":"getName",
"count":4,
"min":2,
"max":4
}]},
"@version":"1",
"@timestamp":"2015-07-14T09:21:18.326Z"
}
{"type":"monitor",
"server":"10.111.222.333",
"host":"abc.de",
"bean":{
"name":"beanName2",
"reseted":"2015-06-05T15:10:00.231Z",
"method":[{
"name":"getProperty",
"count":4,
"min":3,
"max":3
}]},
"@version":"1",
"@timestamp":"2015-07-14T09:21:18.326Z"
}
...
为了分离“方法”,我添加了另一个分离滤波器:
split {
field => "bean"
}
split {
field => "bean.method"
}
但是那样我得到一个错误信息:
filterworker中的异常{“exception”=>#LogStash :: ConfigurationError:只有String和Array类型是可拆分的。 字段:bean.method的类型= NilClass
我无法访问对象“bean”中的数组“方法”。 我尝试了不同的符号,但没有运气。 是否可以访问该数组,可能它尚未受到支持?
下面的代码应该做你想做的,并为每个方法返回一个事件:
filter {
if !("splitted_beans" in [tags]) {
json {
source => "message"
}
split {
field => "bean"
add_tag => ["splitted_beans"]
}
}
if ( "splitted_beans" in [tags] and [bean][method] ) {
split {
field => "bean[method]"
}
}
}
第二个条件检查第一个方法是否成功,以及bean中是否存在方法。 所以它适用于没有方法的bean。
链接地址: http://www.djcxy.com/p/86665.html上一篇: How to split a JSON array inside an object
下一篇: how to include views in asp.net 5 class library nuget package