この Topic では、一般的な JMESPath 構文について説明し、その使用例を示します。
JMESPath は、JSON 用の拡張クエリおよびコンピューティング言語です。JMESPath を使用して、JSON データを抽出、計算、変換できます。詳細については、「JMESPath チュートリアル」をご参照ください。
データ変換では、json_select、e_json、および e_split 関数を JMESPath と共に使用して、フィールドまたは JSON 式の値を取得したり、特定の値を計算したりできます。構文は次のとおりです。
json_select(value, "jmes_expression", ...)
e_json(field_name, jmes="jmes_expression", ...)
e_split(field_name, ... jmes="jmes_expression", ...)これらの関数の使用方法の詳細については、「json_select」、「e_json」、および「e_split」をご参照ください。
フィールドのキーを使用して値を取得する
元のログ
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"}]}変換構文
# state が 'running' である machines から 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:5json_data:{"a": ["b", "c", "d", "e", "f"]} no-empty:true