Is there a possibility to group items by a specific attribute via JSONata
For example grouping following json by the values form the resultDate.
"Export": [
{
"code": "18724-100",
"resultDate": "11.03.2018 13:11:48"
},
{
"code": "18724-5",
"resultDate": "11.03.2018 13:11:48"
},
{
"code": "18724-99",
"resultDate": "14.03.2018 14:11:48"
}
]
To generate the following output:
"Export": [
{
"resultDate": "11.03.2018 13:11:48",
"codes": [
{
"code": "18724-100"
},
{
"code": "18724-5"
}
]
},
{
"resultDate": "14.03.2018 13:11:48",
"codes": [
{
"code": "18724-99"
}
]
}
]
Maybe with a reduce function that groups the elements similar as described in this blog post. https://www.datchley.name/getting-functional-with-javascript-part-2/
The following expression can be used for this:
{
"Export": Export{resultDate: code[]} ~> $each(function($v, $k) {
{
"resultDate": $k,
"codes": $v.{"code": $}
}
})
}
The first part Export{resultDate: code[]}
groups the data by resultDate
then the $each
function iterates over the name/value pairs to produce the output. Expressions like this will probably be easier once the $distinct
function is added to the language (https://github.com/jsonata-js/jsonata/issues/117)