JsonPath

jsonpath

jsonpaht是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,jsonpath对于JSON来说,相当于Xpath对于xml。

官方文档

  • jsonpath和xpath对比
Xpath JSONPath 描述
/ $ 根节点
. @ 现行节点
. . or [ ] 取子节点
.. NA 取父节点,Jsonpath不支持
// .. 不管位置,选择所有符合条件节点
* * 匹配所有元素节点
@ NA 根据属性访问,Json不支持,因为json是个key-value递归结构,不需要属性访问
[ ] [ ] 迭代器标示(可进行简单的迭代操作,如数组下标,根据内容选值等)
| [, ] 支持多选迭代操作
[ ] ?() 支持过滤操作
NA () 支持表达式计算
() NA 分组,Jsonpath不支持
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{ "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
}
}
}
xpath Jsonpath result
/store/book/author $.store.book[*].author the author of all books in the store
//author $..author all authors
/store/* $.store.* all things in store,which are some books and a red bicycle.
/store//price $.store..price the price of everything in the store
//boook[3] $..book[2] the third book
//book[last()] $..book[@.length-1] or $..book[-1:] the last book in order
//book[position()<3] $..book[0,1] or $..book[:2] the first two books
//book[isbn] $..book[?(@.isbn)] filter all books with isbn number
//book[price<10] $..book[?(@price<10)] filter all books cheapier than 10
//* $..* all elements in XML document. All members of JSON structure.