使用jsonpath函数min(),max()或sum() [英] Functions min(), max() or sum() with jsonpath
问题描述
根据 GitHub上的JsonPath ,应该可以访问max(),min( )或数组的sum(),但我不知道如何.使用以下示例数据:
{
"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
}
}
}
我希望它像
$..book.length
我正在尝试
$..price.sum
但是那没做.
有人可以帮助我吗?
来自文档:
可以在路径的尾部调用函数-函数的输入是路径表达式的输出
鉴于此语句,您可能希望上面提供的示例JSON与诸如$.store..price.max()
的表达式一起使用,但此表达式不起作用,而是引发错误:
聚集函数尝试使用空数组计算值
您可以在GitHub上的 JsonPath问题中了解有关此内容的更多信息. /p>
同时,这是一个有效的示例.
给出以下JSON:
{
"price": [
1.0,
2.0
]
}
JsonPath函数的工作方式如下:
-
$..price.min()
返回[1.0]
-
$..price.max()
返回[2.0]
-
$..price.sum()
返回[3.0]
以上内容已使用在线评估程序进行了验证.
According to JsonPath on GitHub it shall be possible to access max(), min() or the sum() of an array but I dont know how. With this exampledata:
{
"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
}
}
}
I would expect it to work like
$..book.length
so im trying
$..price.sum
but that didn't do the job.
Can someone help me?
From the docs:
Functions can be invoked at the tail end of a path - the input to a function is the output of the path expression
Given this statement, you might expect the example JSON you supplied above to work with an expression such as $.store..price.max()
but this expression does not work, instead it throws an error:
Aggregation function attempted to calculate value using empty array
You can read more about that in this JsonPath issue on GitHub.
Meanwhile, here's an example which does work.
Given the following JSON:
{
"price": [
1.0,
2.0
]
}
The JsonPath functions work as follows:
$..price.min()
returns[1.0]
$..price.max()
returns[2.0]
$..price.sum()
returns[3.0]
The above have been verified using the online evaluator.
这篇关于使用jsonpath函数min(),max()或sum()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!