JSONPath语法概述。
JSONPath | 描述 |
---|---|
$ | 根对象/元素 |
@ | 当前对象/元素 |
. or [] | 子对象/元素 |
.. | 取当前对象/元素下所有对象/元素 |
* | 所有对象/元素 |
[] | 迭代器标示,如数组下标 |
[,] | 支持迭代器中做多选 ,如[1,2] ['A','B'] |
[start:end:step] | 数组切片操作符 |
?() | 筛选表达式 |
() | 脚本表达式 |
下面有一个简单的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
}
}
}
练习JSONPath表达式从上面JSON中获取数据
JSONPath | Result |
---|---|
$.store.book[*].author | 获取store对象内,所有books组中author的值 |
$..author | 递归获取根对象内,键为author的值 |
$.store.* | 获取store对象内的值 |
$.store..price | store对象内,递归获取键为price的值 |
$..book[2] | 根对象下递归获取,books第3组的值([0]为第一组) |
$..book[-1:] | 根对象下递归获取,books最后组的值 |
$..book[0,1] | 根对象下递归获取,books第1,2组的值 |
$..book[:2] | 根对象下递归获取,books第1,2组的值 |
$..book[?(@.isbn)] | 根对象下递归获取book组内,含有isbn键的组 |
$..book[?(@.price<10)] | 根对象下递归获取book组内,price键的值小于10的组 |
$..* | 根对象下递归获取所有键的值 |
$..book[?(@.price<10)] .['title','author'] | 根对象下递归获取book组内,price键的值小于10的title和author对象 (jmeter内支持) |
JSONPath在线解析器
https://jsonpath.curiousconcept.com/