Solon v3.6.0

snack - JsonPath 应用参考

</> markdown

1、JsonPath 应用参考

查询

ONode oNode = ONode.ofBean(store);

oNode.select("$..book[?@.tags contains 'war'].first()").toBean(Book.class); //RFC9535 规范,可以没有括号
oNode.select("$..book[?(!(@.category == 'fiction') && @.price < 40)].first()").toBean(Book.class);
oNode.select("$.store.book.count()");

生成

ONode.ofJson(store).create("$.store.book[0].category").toJson();

删除

ONode.ofBean(store).delete("$..book[-1]");

2、语法简单参考(后续补充完整的)

语法描述示例
$根节点定位$..a
..深度遍历$..a
*展开所有子节点$..*, $.user.*
.name, ['name']属性定位$.user.name, $['user']['name']
[index]索引定位
[index,index,index]多索引定位$.data.list[1,4]
[start:end:step?]区间定位(step 默认为 1)$.data.list[1:4]
[?...]条表式过滤$..b[?(@.c == 12)]
$[?(@.c =~ /a+/)]
$.data.list[?(@ in $..ary2[0].a)]

3、内置的操作符与函数

操作符

操作符描述示例
startsWith开始为$.list[?(@.mobile startsWith '1861111')]
endsWith结尾为$.list[?(@.mobile endsWith '4444')]
contains包含$.list[?(@.mobile contains '1919')]
in$.list[?(@.label in [1,2])]
nin不在$.list[?(@.label nin [1,2])]
"=~匹配$.list[?(@.name =~ /a+/)]
==等于$.list[?(@.score == 12)]
!=不等于$.list[?(@.score != 12)]
>大于$.list[?(@.score > 12)]
<小于$.list[?(@.score < 12)]
>=大于等于$.list[?(@.score >= 12)]
<=小于等于$.list[?(@.score <= 12)]

函数

函数描述示例
- 聚合函数
min最小值$.list.min()
max最大值$.list.max()
avg平均值$.list.avg()
sum求和$.list.sum()
- 集合函数
size取大小$.list.size()
keys取主键$.map.keys()
first取第一个值$.list.first()
last取第二个值$.list.last()
- 字符串函数
length长度$.name.length()
upper变大写$.name.upper()
lower变小写$.name.lower()
trim去掉头尾的空$.name.trim()