使用 JSONPath 查找最昂贵的书
Find most expensive book using JSONPath
以 Goessner 为例 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
}
}
}
我可以使用以下查询获得一本书的最高价格:
$.max($.store.book[*].price))
.
响应:22.99
。
如果我查询这个价格:$.store.book[?(@.price ==22.99)]
回复:
[
{
"category" : "fiction",
"author" : "J. R. R. Tolkien",
"title" : "The Lord of the Rings",
"isbn" : "0-395-19395-8",
"price" : 22.99
}
]
但是尝试将 max 表达式内联到查询中只会出现语法错误:
$.store.book[?(@.price ==$.max($.store.book[*].price)))]
在单个 JSON路径查询中这可能吗?
在Jayway JSONPath中可以在路径的尾端调用函数
$.store.book[?(@.price == $..book[*].price.max())]
在线工具:https://jsonpath.herokuapp.com/
注意:可能不适用于其他实现。
以 Goessner 为例 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
}
}
}
我可以使用以下查询获得一本书的最高价格:
$.max($.store.book[*].price))
.
响应:22.99
。
如果我查询这个价格:$.store.book[?(@.price ==22.99)]
回复:
[
{
"category" : "fiction",
"author" : "J. R. R. Tolkien",
"title" : "The Lord of the Rings",
"isbn" : "0-395-19395-8",
"price" : 22.99
}
]
但是尝试将 max 表达式内联到查询中只会出现语法错误:
$.store.book[?(@.price ==$.max($.store.book[*].price)))]
在单个 JSON路径查询中这可能吗?
在Jayway JSONPath中可以在路径的尾端调用函数
$.store.book[?(@.price == $..book[*].price.max())]
在线工具:https://jsonpath.herokuapp.com/
注意:可能不适用于其他实现。