How JMESPath group by and summary data
shps951023 opened this issue · 2 comments
How JMESPath group by and summary data
input data :
[
{
"value" : 15,
"name" : "app1"
},
{
"value" : 25,
"name" : "app1"
},
{
"value" : 30,
"name" : "app2"
},
{
"value" : 45,
"name" : "app2"
}
]
expected result :
[
{
"name" : "app1",
"summary_value" : 40
},
{
"name" : "app2",
"summary_value" : 75
},
]
what I've tried & thought :
It tried to below code but it not work and return Script error.
message
const testData =
[
{
"value" : 15,
"name" : "app1"
},
{
"value" : 25,
"name" : "app1"
},
{
"value" : 30,
"name" : "app2"
},
{
"value" : 45,
"name" : "app2"
}
];
console.log( jmespath.search(testData, '[].{name:name,summary_value:sum([?name = name].value)}') );
Demo - JSFiddle - Code Playground
PS : my env is ubuntu and support bash
Hey, the issue here is that once you are in the projection you can't get back to the parent level of the data. So the ?name == name
conditional clause isn't going to do what you think it should do.
I don't know how you can acheive this with standard JMESPath, but with my fork I've added a decorate
function so that you can add your own functions to JMESPath's internal function table. Here is a working example using your testData
above that uses the groupBy
function from lodash:
https://codesandbox.io/s/zealous-dew-lr9g3?file=/src/index.js
@darrenmothersele thanks!
It looks like JMESPath need to custom function to do it.