全部產品
Search
文件中心

Simple Log Service:JMES文法

更新時間:Aug 29, 2025

本文主要介紹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