一、JMESPath
JMESPath,jsonpath 目的 如何从JSON文档中提取元素
-
查询key对应的value
{ 'ops': { 'functionA': {'numArgs': 2}, 'functionB': {'numArgs': 3}, 'functionC': {'variadic': True} } }
jmespath.search('b', source) bar
-
使用.操作符
{"a": {"b": {"c": {"d": "value"}, "e": "value"}}}
jmespath.search('a.b.c', source)
-
下标操作,仅用于数组
["a", "b", "c", "d", "e", "f"]
jmespath.search("[1]",source)
-
下标和.操作符混合操作
{'a': {'b': {'c': [{'d': [0, [1, 2]]}, {'d': [3, 4]}]}}}
jmespath.search('a.b.c[0].d[1][0]',source)
-
切片
["a", "b", "c", "d", "e", "f"]
jmespath.search("[1:3]",source)
-
投影 其实就是初始时定义好格式,然后按照格式的方式进行取值。
List列表、Slice切片、字典对象、Flatten 正则、Filter 过滤条件
注意:取列表用[],取字典用.
{ 'people': [ {'first': 'James', 'last': 'd'}, {'first': 'Jacob', 'last': 'e'}, {'first': 'Jayden', 'last': 'f'}, {'missing': 'different'} ], 'foo': {'bar': 'baz'} }
jmespath.search('people[*].first', source5) 列表和切片
{ 'ops': { 'functionA': {'numArgs': 2}, 'functionB': {'numArgs': 3}, 'functionC': {'variadic': True} } }
jmespath.search('ops.*.numArgs', source) 支持 列表字典 语法为the*syntax.
{ 'people': [ { 'general': {'id': 100, 'age': 20, 'other': 'foo', 'name': 'Bob'}, 'history': {'first_login': '2014-01-01', 'last_login': '2014-01-02'} }, { 'general': {'id': 101, 'age': 30, 'other': 'bar', 'name': 'Bill'}, 'history': {'first_login': '2014-05-01', 'last_login': '2014-05-02'} } ] }
jmespath.search("people[?general.age > `20`].general | [0]", source7) 带过滤条件 ==, !=, <, <=, >, >=: 大于20的年龄键的常规键的值
二、JMESPath 简单应用
-
任何年龄键值大于 20 的元素
支持过滤器表达式 ( [? < expr > ] ){ 'people': [ {'age': 20, 'other': 'foo', 'name': 'Bob'}, {'age': 25, 'other': 'bar', 'name': 'Fred'}, {'age': 30, 'other': 'baz', 'name': 'George'} ] }
people[?age > "20"].[name, age] people[?age > '20'].{age: age, tags: tags[0], name: name}
-
处理嵌套数据
支持 操作符、多选列表、过滤器和管道{ "reservations": [ { "instances": [ { "type": "small", "state": {"name": "running"}, "tags": [ {"Key": "Name","Values": ["Web"]}, {"Key": "version","Values": ["1"] } ] }, { "type": "large", "state": {"name": "stopped"}, "tags": [ {"Key": "Name","Values": ["Web"]}, {"Key": "version","Values": ["1"]} ] } ] }, { "instances": [ { "type": "medium", "state": {"name": "terminated"}, "tags": [ {"Key": "Name","Values": ["Web"]}, {"Key": "version","Values": ["1"]} ] }, { "type": "xlarge", "state": {"name": "running"}, "tags": [ {"Key": "Name","Values": ["DB"]}, {"Key": "version","Values": ["1"] } ] } ] } ] }
基础获取数据 reservations[].instances[].[tags[?Key=='Name'].Values[] | [0], type, state.name] 过滤嵌套数据 people[?general.id==`100`].general | [0]
-
sort_by
指定字符排序获取最大值max_by
、最小值min_by
{ 'Contents': [ {'Date': '2014-12-21T05:18:08.000Z', 'Key': 'logs/bb', 'Size': 303}, {'Date': '2014-12-20T05:19:10.000Z', 'Key': 'logs/aa', 'Size': 308}, {'Date': '2014-12-20T05:19:12.000Z', 'Key': 'logs/qux', 'Size': 297}, {'Date': '2014-11-20T05:22:23.000Z', 'Key': 'logs/baz', 'Size': 329}, {'Date': '2014-12-20T05:25:24.000Z', 'Key': 'logs/bar', 'Size': 604}, {'Date': '2014-12-20T05:27:12.000Z', 'Key': 'logs/foo', 'Size': 647} ] }
按照日期排序 sort_by(Contents, &Date)[*].{Key: Key, Size: Size}
-
稍微复杂应用
{ 'locations': [ {'name': 'Seattle', 'state': 'WA'}, {'name': 'New York', 'state': 'NY'}, {'name': 'Bellevue', 'state': 'WA'}, {'name': 'Olympia', 'state': 'WA'} ] }
筛选、拼接 locations[?state == 'WA'].name | sort(@)[-2:] | {WashingtonCities: join(', ', @)}
三、jsonpath 与xpath 区别
以下是一些基本示例,可让您对它的功能有所了解: