すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:JMESPath 構文

最終更新日:Aug 28, 2024

このトピックでは、一般的なJMESPathの構文について説明し、構文の使用方法について例を示します。

JMESPathは、JSON用の強化されたクエリおよびコンピューティング言語です。 JMESPathを使用して、JSONデータの抽出、計算、変換を行うことができます。 詳細については、『JMESPath Tutorial』をご参照ください。

データ変換機能では、 json_select, e_json, and e_split の各関数をサポートしています。これらの関数でJMESPathを使用し、フィールドやJSON式の値を抽出したり、特定の値を計算することができます。変換ルール:

json_select(Value, "JMESPath expression", ...)
e_json(Field name, jmes="JMESPath expression", ...)
e_split(Field name, ... jmes="JMESPath expression", ...)

For more information about how to use the functions, see json_select, e_json, and e_split.

Obtain a value by using the key of a field

  • 生ログ

    json_data: {"a":"foo","b":"bar","c":"baz"}
  • 変換ルール

    # Obtain the value of a from the JSON expression. 
    e_set("a1", json_select(v("json_data"), "a"))
    # Obtain the value of b from the JSON expression. 
    e_set("b1", json_select(v("json_data"), "b"))
    # Obtain the value of c from the JSON expression. 
    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"}}}}
  • 変換ルール

    # Obtain the value of d from the JSON expression. 
    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"]}
  • 変換ルール

    # Obtain the value of the a field starting from slice 2. The value at slice 0 is b, and the value at slice 1 is c. 
    e_set("key", json_select(v("json_data"), "a[2:]"))
  • 結果

    json_data:{"a": ["b", "c", "d", "e", "f"]}
    key:["d", "e", "f"]

Obtain a value by combining the preceding methods

  • 生ログ

    json_data:{"a": {"b": {"c": [{"d": [0, [1, 2]]}, {"d": [3, 4]}]}}}
  • 変換ルール

    # c[0] specifies {"d": [0, [1, 2]]}. d[1] specifies [1, 2]]. The return value is 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"}}
    • 変換ルール

      # Obtain the values of the first fields from the people list. 
      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}}}
    • 変換ルール

      # Obtain the values of the numArgs fields from the ops list. 
      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"}]}
    • 変換ルール

      # Obtain the values of the name fields for which the state field is running from the machines list. 
      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"]

Extract multiple values

  • 生ログ

    json_data:{"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}}]}
  • 変換ルール

    # Obtain the values of the name and state fields from the people list. 
    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"]]

Calculate the number of elements in an array

  • 生ログ

    json_data:{"a": ["b", "c", "d", "e", "f"]}
  • 変換ルール

    # Obtain the number of elements in the a array. 
    e_set("key", json_select(v("json_data"), "length(a)"))
    # Obtain the number of elements in the a array. If the result of length(a) is greater than 0, set the no-empty parameter to 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