本文主要介绍JMES的常用语法和示例。

JMES是一个增强型的JSON查询计算语言,不仅可以对JSON数据进行提取,还可以做计算与转换。关于JMES语法的详细介绍请参见JMES Tutorial

数据加工中的json_selecte_jsone_split函数支持通过JMES提取字段或JSON表达式的值,或者通过JMES计算特定的值。其用法为:
json_select(值, "jmes表达式", ...)
e_json(字段名, jmes="jmes表达式", ...)
e_split(字段名, ... jmes="jmes表达式", ...)
函数的具体用法请参见json_selecte_jsone_split

通过字段的key获取值

  • 原始日志
    json_data: {"a":"foo","b":"bar","c":"baz"}
  • 加工语法
    # 获取JSON表达式中a的值。
    e_set("a1", json_select(v("json_data"), "a"))
    # 获取JSON表达式中b的值。
    e_set("b1", json_select(v("json_data"), "b"))
    # 获取JSON表达式中c的值。
    e_set("c1", json_select(v("json_data"), "c"))
  • 加工结果
    a1:foo
    b1:bar
    c1:baz
    json_data:{"a":"foo","b":"bar","c":"baz"}

通过层级访问获取值

  • 原始日志
    json_data:{"a": {"b":{"c":{"d":"value"}}}}
  • 加工语法
    # 获取JSON表达式中d的值。
    e_set("e", json_select(v("json_data"), "a.b.c.d"))
  • 加工结果
    e:value
    json_data:{"a": {"b":{"c":{"d":"value"}}}}

通过切片操作获取值

  • 原始日志
    json_data:{"a": ["b", "c", "d", "e", "f"]}
  • 加工语法
    # 获取字段a中从切片2开始的值。切片0值为b,切片1值为c。
    e_set("key", json_select(v("json_data"), "a[2:]"))
  • 加工结果
    json_data:{"a": ["b", "c", "d", "e", "f"]}
    key:["d", "e", "f"]

多种用法综合使用

  • 原始日志
    json_data:{"a": {"b": {"c": [{"d": [0, [1, 2]]}, {"d": [3, 4]}]}}}
  • 加工语法
    # c[0]表示{"d": [0, [1, 2]]}部分;d[1]表示[1, 2]]。返回值为1。
    e_set("key", json_select(v("json_data"), "a.b.c[0].d[1][0]"))
  • 加工结果
    json_data:{"a": {"b": {"c": [{"d": [0, [1, 2]]}, {"d": [3, 4]}]}}}
    key:1

通过投影获取值

  • 示例1
    • 原始日志
      json_data:{"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"}}
    • 加工语法
      # 获取people列表中first的值。
      e_set("key", json_select(v("json_data"), "people[*].first"))
    • 加工结果
      json_data:{"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"}}
      key:["James", "Jacob", "Jayden"]
  • 示例2
    • 原始日志
      json_data:{"ops": {"functionA": {"numArgs": 2},"functionB": {"numArgs": 3},"functionC": {"variadic": true}}}
    • 加工语法
      # 获取ops中numArgs的值。
      e_set("key", json_select(v("json_data"), "ops.*.numArgs"))
    • 加工结果
      json_data:{"ops": {"functionA": {"numArgs": 2},"functionB": {"numArgs": 3},"functionC": {"variadic": true}}}
      key:[2, 3]
  • 示例3
    • 原始日志
      json_data:{"machines": [{"name": "a", "state": "running"},{"name": "b", "state": "stopped"},{"name": "c", "state": "running"}]}
    • 加工语法
      # 获取machines中running状态的name值。
      e_set("key", json_select(v("json_data"), "machines[?state=='running'].name"))
    • 加工结果
      json_data:{"machines": [{"name": "a", "state": "running"},{"name": "b", "state": "stopped"},{"name": "c", "state": "running"}]}
      key:["a", "c"]

多值选取

  • 原始日志
    json_data:{"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}}]}
  • 加工语法
    # 获取people的name及state值。
    e_set("key", json_select(v("json_data"), "people[].[name, state.name]"))
  • 加工结果
    json_data:{"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}}]}
    key:[["a", "up"], ["b", "down"]]

计算数组长度

  • 原始日志
    json_data:{"a": ["b", "c", "d", "e", "f"]}
  • 加工语法
    # 获取数组a的长度。
    e_set("key", json_select(v("json_data"), "length(a)"))
    # 获取数组a的长度,若length(a) > 0, 设置no-empty字段为true。
    e_if(json_select(v("json_data"), "length(a)", default=0), e_set("no-empty", true))
  • 加工结果
    json_data:{"a": ["b", "c", "d", "e", "f"]}
    key:5
    json_data:{"a": ["b", "c", "d", "e", "f"]}
    no-empty:true