jsonpath提取表达式笔记

发布时间 2023-06-12 17:12:27作者: 青域

已知 result 返回数据为json格式,示例如下:

{ "data": {
     "expensive":20,
"book": [{ "id": 1, "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "id": 2, "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99, "isbn": "0-553-21311-3" } ] } }

各类提取用法:

【基础提取】

序号 含义 提取表达式
1 提取返回的所有id $.data.book[*].id
2 提取返回的第一个id $.data.book[0].id
3 提取返回的最后一个id $.data.book[-1].id
4 size   提取数组长度 $..book[*].id.
5 [m:n]  截片,提取最后两个id $..book[-2:]
6 提取具有isbn属性的id $..book[?(@isbn)]

 

【条件提取】

1 ==  提取符合指定值的id $.data.book[?(@title== 'Sword of Honour')].id
2 <   提取符合比较值的id $.data.book[?(@price >= 15)].id
3 <   提取价格不高于指定字段的书 $.data.book[?(@price < ${expensive})]
4 in   提取符合包含数据的id $.data.book[?(@title in  ['Sword of Honour','Sayings of the Century'])].id
5 nin  提取符合不包含数据的id $.data.book[?(@title nin  ['Sword of Honour','Sayings of the Century'])].id
6 anyof    
7 noneof   
8 && 多条件提取 $.data.book[?(@title== 'Sword of Honour' && @price <= 15)].id

【函数提取】

 1 length()       提取字段长度 $..book.length()
 2 max()          提取字段最大值 $..price.max()
 3 min()           提取字段最小值 $..price.min()
 4 length()       提取字段长度 $..book.length()
 5 avg()           提取字段平均值 $..book[*].price.avg()
 6 stddev()      提前字段标准差 $..book[*].price.stddev()

【正则提取】

序号 含义 提取表达式
1 =~     匹配表达式 以Century结尾 $.data.book[?(@title =~  /.*Century/])].id
2 =~     匹配表达式 以Century开头 $.data.book[?(@title =~  /Sword.*?/])].id
3 =~     匹配符合其他正则表达式 $.data.book[?(@title =~  /.*REES/i])].id
4 =~     提取book中满足 title为纯数字的 $.data[*].book[?(@.title=~ /\\d+/i)]
5 =~     提取book中满足 title匹配纯数字~纯数字的 $.data[*].book[?(@.title=~ /\\d+~\\d+/i)

metersphere中${id}匹配多个值后,引用第一个值表达式为${id_1}