I have one JSON data that I want to restructure the data using JSONata query
{
"response": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 46
},
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 275
},
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 199.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer ",
"bookingvalue": 200
}
]
}
And I'm trying to use JSONata to change it to something more like this:
{
"output": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 520.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer",
"bookingvalue": 200
}
]
}
i tried with this query but i am getting only enterpriseid and enterprise not getting bookingvalue
{
"output": $distinct(response.{
"enterpriseid" : enterpriseid,
"enterprise": enterprise,
"bookingvalue": $sum($filter(response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
} )
}
You need to use double dollar signs ($$
) to break out of $distinct
's context for the sum of all filtered items.
$$
The root of the input JSON. Only needed if you need to break out of the current context to temporarily navigate down a different path.
For example:
{
"output": $distinct(response.{
"enterpriseid" : enterpriseid,
"enterprise": enterprise,
"bookingvalue": $sum($filter($$.response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
})
}
(Note the double dollar sign $$
usage inside of $filter
.)
Which outputs:
{
"output": [
{
"enterpriseid": 53898,
"enterprise": " Absolute F and B Facilities Management LLC ",
"bookingvalue": 520.5
},
{
"enterpriseid": 53899,
"enterprise": " testing Buyer ",
"bookingvalue": 200
}
]
}
Working Example: https://try.jsonata.org/Y-7YTZ_eJ