Functions min(), max() or sum() with jsonpath

 

问题

https://stackoverflow.com/questions/52817156/functions-min-max-or-sum-with-jsonpath

According toJsonPath on GitHubit 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":"NigelRees",
"title":"SayingsoftheCentury",
"price":8.95
},
{
"category":"fiction",
"author":"EvelynWaugh",
"title":"SwordofHonour",
"price":12.99
},
{
"category":"fiction",
"author":"HermanMelville",
"title":"MobyDick",
"isbn":"0-553-21311-3",
"price":8.99
},
{
"category":"fiction",
"author":"J.R.R.Tolkien",
"title":"TheLordoftheRings",
"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?

解答

Json是记录集合,需要对各记录集合。示例代码以sum为例,minmax类似。jsonPath 只支持简单集合的聚合,以及枚举再聚合,不支持记录集合的聚合。

Java 下的开源包 SPL 很容易写,只要 1 句:


A

1

=json(file("data.json").read()).store.book.sum(price)

SPL 提供了 JDBC 供 Java 调用,把上面的脚本存为 sum.splx,在 Java 中以存储过程的方式调用脚本文件:

Class.forName("com.esproc.jdbc.InternalDriver");

con= DriverManager.getConnection("jdbc:esproc:local://");

st = con.prepareCall("call sum()");
st.execute();

SPL 源代码:https://github.com/SPLWare/esProc

问答搜集