...
JSONPath | 描述 |
---|---|
$ | 根对象或元素。 |
@ | 当前对象或元素。 |
. or [] | 子元素操作符。
|
.. | 递归匹配所有子元素。 |
* | 通配符. 匹配所有对象或元素。 |
[] | 下标运算符,JsonPath索引从0开始。 |
[,] | 连接运算符,将多个结果拼成数组返回,JSONPath允许使用别名。 |
[start:end:step] | 数组切片运算符。 |
?() | 过滤器(脚本)表达式。 |
(<expr>) | 脚本表达式。 |
要点:
1. 根对象使用 $
来表示,而无需区分是对象还是数组。
...
$.store.book[?(@.price < 10)].title 表示获取价格小于 10 的所有 book 的 title。
函数
函数 | 描述 | 输出类型 |
---|---|---|
min() | ||
max() | ||
avg() | ||
stddev() | ||
length() | ||
sum() |
示例
例如有下面的json结构:
代码块 | ||
---|---|---|
| ||
{
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}, {
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}, {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
} |