Bu konu, yaygın JMESPath sözdizimini açıklar ve bu sözdiziminin nasıl kullanılacağına ilişkin örnekler sunar.
JMESPath, JSON için geliştirilmiş bir sorgulama ve hesaplama dilidir. JSON verilerini ayıklamak, hesaplamak ve dönüştürmek amacıyla JMESPath'i kullanabilirsiniz. Daha fazla bilgi için JMESPath Öğreticisi konusuna bakın.
Veri dönüştürme işlemlerinde, alanların veya JSON ifadelerinin değerlerini almak ya da belirli değerleri hesaplamak üzere json_select, e_json ve e_split işlevlerini JMESPath ile birlikte kullanın. Sözdizimi aşağıdaki gibidir:
json_select(value, "jmes_expression", ...)
e_json(field_name, jmes="jmes_expression", ...)
e_split(field_name, ... jmes="jmes_expression", ...)
Bu işlevlerin kullanımı hakkında daha fazla bilgi için json_select, e_json ve e_split konularına bakın.
Bir alanın anahtarını kullanarak değer elde etme
-
Ham günlük
json_data: {"a":"foo","b":"bar","c":"baz"} -
Dönüştürme sözdizimi
# Get the value of a from the JSON expression. e_set("a1", json_select(v("json_data"), "a")) # Get the value of b from the JSON expression. e_set("b1", json_select(v("json_data"), "b")) # Get the value of c from the JSON expression. e_set("c1", json_select(v("json_data"), "c")) -
Sonuç
a1:foo b1:bar c1:baz json_data:{"a":"foo","b":"bar","c":"baz"}
İç içe geçmiş bir değeri elde etme
-
Ham günlük
json_data:{"a": {"b":{"c":{"d":"value"}}}} -
Dönüştürme sözdizimi
# Get the value of d from the JSON expression. e_set("e", json_select(v("json_data"), "a.b.c.d")) -
Sonuç
e:value json_data:{"a": {"b":{"c":{"d":"value"}}}}
Verileri dilimleyerek değer elde etme
-
Ham günlük
json_data:{"a": ["b", "c", "d", "e", "f"]} -
Dönüştürme sözdizimi
# Get the values from field a, 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:]")) -
Sonuç
json_data:{"a": ["b", "c", "d", "e", "f"]} key:["d", "e", "f"]
Birleşik Kullanım
-
Ham günlük
json_data:{"a": {"b": {"c": [{"d": [0, [1, 2]]}, {"d": [3, 4]}]}}} -
Dönüştürme sözdizimi
# c[0] represents the {"d": [0, [1, 2]]} part. d[1] represents [1, 2]]. The return value is 1. e_set("key", json_select(v("json_data"), "a.b.c[0].d[1][0]")) -
Sonuç
json_data:{"a": {"b": {"c": [{"d": [0, [1, 2]]}, {"d": [3, 4]}]}}} key:1
Projeksiyon kullanarak değer elde etme
-
Örnek 1
-
Ham günlük
json_data:{"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"}} -
Dönüştürme sözdizimi
# Get the values of the first fields from the people list. e_set("key", json_select(v("json_data"), "people[*].first")) -
Sonuç
json_data:{"people": [{"first": "James", "last": "d"},{"first": "Jacob", "last": "e"},{"first": "Jayden", "last": "f"},{"missing": "different"}],"foo": {"bar": "baz"}} key:["James", "Jacob", "Jayden"]
-
-
Örnek 2
-
Ham günlük
json_data:{"ops": {"functionA": {"numArgs": 2},"functionB": {"numArgs": 3},"functionC": {"variadic": true}}} -
Dönüştürme sözdizimi
# Get the values of the numArgs fields from ops. e_set("key", json_select(v("json_data"), "ops.*.numArgs")) -
Sonuç
json_data:{"ops": {"functionA": {"numArgs": 2},"functionB": {"numArgs": 3},"functionC": {"variadic": true}}} key:[2, 3]
-
-
Örnek 3
-
Ham günlük
json_data:{"machines": [{"name": "a", "state": "running"},{"name": "b", "state": "stopped"},{"name": "c", "state": "running"}]} -
Dönüştürme sözdizimi
# Get the name values from machines where the state is 'running'. e_set("key", json_select(v("json_data"), "machines[?state=='running'].name")) -
Sonuç
json_data:{"machines": [{"name": "a", "state": "running"},{"name": "b", "state": "stopped"},{"name": "c", "state": "running"}]} key:["a", "c"]
-
Birden çok değeri ayıklama
-
Ham günlük
json_data:{"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}}]} -
Dönüştürme sözdizimi
# Get the name and state values from people. e_set("key", json_select(v("json_data"), "people[].[name, state.name]")) -
Sonuç
json_data:{"people": [{"name": "a","state": {"name": "up"}},{"name": "b","state": {"name": "down"}}]} key:[["a", "up"], ["b", "down"]]
Bir dizideki öğe sayısını hesaplama
-
Ham günlük
json_data:{"a": ["b", "c", "d", "e", "f"]} -
Sözdizimi
# Get the length of array a. e_set("key", json_select(v("json_data"), "length(a)"))# Get the length of array a. If length(a) > 0, set the no-empty field to true. e_if(json_select(v("json_data"), "length(a)", default=0), e_set("no-empty", true))Sonuç
json_data:{"a": ["b", "c", "d", "e", "f"]} key:5json_data:{"a": ["b", "c", "d", "e", "f"]} no-empty:true