Logstash解析Json array

logstash解析json数组是一种常见的需求,我以网上一组数据为例来描述

我们的数据test.json内容如下:(此处我linux上的json文本需要是compact的)

{"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"}]}

为了方便看清楚内容,我们format后查看:

{
"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字段下是一个json数组,解析这种json数组,我们需要借用logstash split filter plugin

测试:单纯地把bean字段下的json拆分出来

我的配置文件如下

input {
        file {
            path => "/usr/share/logstash/private.cond/split.json"
            codec => "json"
            start_position => "beginning"
            sincedb_path => "/dev/null"
        }
}
filter {
         json {
            source => "message"
         }
         split {
            field => "bean"
         }
}

output {
        stdout {
            codec => rubydebug
        }
}

我们得到如下输出结果

{
      "@version" => "1",
        "server" => "10.111.222.333",
          "type" => "monitor",
          "bean" => {
           "name" => "beanName1",
         "method" => [
            [0] {
                  "min" => 3,
                 "name" => "getAllXY",
                "count" => 5,
                  "max" => 5
            },
            [1] {
                  "min" => 2,
                 "name" => "getName",
                "count" => 4,
                  "max" => 4
            }
        ],
        "reseted" => "2015-06-05T15:10:00.192Z"
    },
          "path" => "/usr/share/logstash/private.cond/split.json",
    "@timestamp" => 2018-08-02T10:36:21.248Z,
          "host" => "abc.de"
}
{
      "@version" => "1",
        "server" => "10.111.222.333",
          "type" => "monitor",
          "bean" => {
           "name" => "beanName2",
         "method" => [
            [0] {
                  "min" => 3,
                 "name" => "getProperty",
                "count" => 4,
                  "max" => 3
            }
        ],
        "reseted" => "2015-06-05T15:10:00.231Z"
    },
          "path" => "/usr/share/logstash/private.cond/split.json",
    "@timestamp" => 2018-08-02T10:36:21.248Z,
          "host" => "abc.de"
}
{
      "@version" => "1",
        "server" => "10.111.222.333",
          "type" => "monitor",
          "bean" => {
        "reseted" => "2015-06-05T15:10:00.231Z",
           "name" => "beanName3"
    },
          "path" => "/usr/share/logstash/private.cond/split.json",
    "@timestamp" => 2018-08-02T10:36:21.248Z,
          "host" => "abc.de"
}

根据输出我们可以看到json数组被我单个拆分出来。

原文地址:https://www.cnblogs.com/yangwenbo214/p/9830949.html

时间: 2024-11-06 21:11:37

Logstash解析Json array的相关文章

Logstash 解析Json字符串,删除json嵌套字段

一.场景:此文以一个简单的json字符串文件为例,描述如何用logstash解析嵌套的json,并删除其中的某些字段 我们在linux中test.json的内容如下: {"timestamp":"2018-08-02T14:42:50.084467+0800","flow_id":364959073190719,"in_iface":"eth1","event_type":"a

浅谈C#手动解析Json格式内容

这个应该算处女贴吧 - - 之前百度了许久基本没有一个满意的json结构的解析类库 想了想还是自己做一个吧 现在我来说下大概的思路 首先我创建了一个 JsonTokener的类 用于处理json字符串的一些操作里面有个枚举 1 public enum JsonCharType 2 { 3 BeginObject = 123, //{ 4 EndObject = 125, //} 5 BeginArray = 91, //[ 6 EndArray = 93, //] 7 DoubleQuote =

Logstash替换字符串,解析json数据,修改数据类型,获取日志时间

在某些情况下,有些日志文本文件类json,但它的是单引号,具体格式如下,我们需要根据下列日志数据,获取正确的字段和字段类型 {'usdCnyRate': '6.728', 'futureIndex': '463.36', 'timestamp': '1532933162361'} {'usdCnyRate': '6.728', 'futureIndex': '463.378', 'timestamp': '1532933222335'} {'usdCnyRate': '6.728', 'futu

logstash解析嵌套json格式数据

logstash解析嵌套json格式数据 1.源文件 1.原日志文件为 2019-10-28 09:49:44:947 [http-nio-8080-exec-23] INFO [siftLog][qewrw123ffwer2323fdsafd] - logTime:2019-10-28 09:49:25.833-receiveTime:2019-10-28 09:49:44.044-{"area":"","frontInitTime":0,&q

[转]解析json:与array,list,map,bean,xml相互转化

一.解析json之net.sf.json 下载地址 使用netsfjson需要导入的包 JSONObject JSONArray JavaBean与json字符串互转 List与json字符串互转 Map与json字符串互转 JSONArray与List互转 JSONArray与数组互转 XML与JSON互转 下载地址 [plain] view plain copy 本次使用版本:http://sourceforge.net/projects/json-lib/files/json-lib/js

不适用工具类解析Json

解析的数据: { "citycode": "101051301", "citydesc": "黑龙江 双鸭山", "cityname": "双鸭山", "data": [ { "date": "2015-08-29", "icon": "d07|n07", "temperatur

Android中解析JSON形式的数据

1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换.JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为. 2.JSON的结构: (1) Name/Value Pairs(无序的):类似所熟知的Keyed list. Hash table.Disctionary和Assoc

Java解析JSON对象

今天学习一下Java 解析 JSON 准备工作 参考文档 1) www.json.org 2 ) 参考公司其他人的代码(这个就不说了) 依赖jar包 1) org.json.jar 提供以下我的网盘链接 http://pan.baidu.com/s/1jG2zSwe 主要的类 JSON主要有两种结构 1)name/value 的集合 2)value 的集合,在大多数语言中的list,array等,这里的 value 个人觉得就是一个JSON,也就是说是JSON的集合 JAVA处理JSON的类 1

java 中解析json步骤

一.   JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧. Json建构于两种结构:     1."名称/值"对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array). 如: