Grouping and Sorting Records in Kumologica
Let's get organized.
Join the DZone community and get the full member experience.
Join For FreeGrouping and sorting of records are common functionalities seen in many microservices and integration requirements. In most use cases, the records fetched from the source system might be in raw form, with all the records treated separately, without having any records grouped as per the functional requirements. Similarly, another functionality that is popularly used is the sorting of records. This can be either sorting based on an existing property in the record, or it can be based on the computation of some properties and then sorting the records based on the result of the computation.
Grouping
Grouping is the functionality of combining records with a common property or attribute as a single unit. This can be based on one property or multiple properties associated with the records. These properties can also be referred to as keys. In the diagram below, we can see cars of different brands grouped based on the their color and type properties. Group 1 has both Ford and BMW since they have the same traits of color and type.
Sorting
Sorting is the functionality of ordering the records based on a property associated with the record. In the diagram below, we can see cars of different brands stacked based on the RegNo order.
How to Group and Sort in Kumologica
In this article, we are going to show you how to group and order records in Kumologica. If you are new to Kumologica, we would recommend going through our articles and YouTube videos to get started.
Kumologica provides two major means of implementing grouping and sorting of records. You may either use a datamapper node or a function node. In this article, we will focus only on datamapper nodes because with a function node, you can devise your own JavaScript logic to achieve the grouping or ordering.
Grouping Records Using datamapper Nodes
Let's assume that the incoming message payload has the following JSON content. We will group the JSON objects (individual cars) based on the Color and Type.
xxxxxxxxxx
{
"Cars" : [
{
"Car": "Ford",
"Color": "Red",
"Type": "Sedan"
},
{
"Car": "BMW",
"Color": "Red",
"Type": "Hatch"
},
{
"Car": "Ford",
"Color": "Black",
"Type": "Hatch"
},
{
"Car": "BMW",
"Color": "Red",
"Type": "Sedan"
}
]
}
You can group the payload easily using the following JSONata expression in the datamapper node.
xxxxxxxxxx
msg.payload.Cars{
Color & Type:[
$.{
"Car" : $.Car,
"Color" : $.Color,
"Type" : $.Type
}]
}
The following will be the response after grouping:
xxxxxxxxxx
{
"RedSedan": [
{
"Car": "Ford",
"Color": "Red",
"Type": "Sedan"
},
{
"Car": "BMW",
"Color": "Red",
"Type": "Sedan"
}
],
"RedHatch": [
{
"Car": "BMW",
"Color": "Red",
"Type": "Hatch"
}
],
"BlackHatch": [
{
"Car": "Ford",
"Color": "Black",
"Type": "Hatch"
}
]
}
Sorting Records Using datamapper Nodes
Let's assume that the incoming message payload has the following JSON payload.
xxxxxxxxxx
{
"Cars" : [
{
"Car": "Ford",
"Color": "Red",
"RegNo": "004"
},
{
"Car": "BMW",
"Color": "Red",
"RegNo": "002"
},
{
"Car": "Ford",
"Color": "Black",
"RegNo": "008"
},
{
"Car": "BMW",
"Color": "Red",
"RegNo": "006"
}
]
}
You can sort the payload in ascending order easily using the following JSONata expression within the datamapper node.
xxxxxxxxxx
msg.payload.Cars^(RegNo).{
"Car" : $.Car,
"Color" : $.Color,
"RegNo" : $.RegNo
}
The following will be the response after sorting:
[
{
"Car": "BMW",
"Color": "Red",
"RegNo": "002"
},
{
"Car": "Ford",
"Color": "Red",
"RegNo": "004"
},
{
"Car": "BMW",
"Color": "Red",
"RegNo": "006"
},
{
"Car": "Ford",
"Color": "Black",
"RegNo": "008"
}
]
Opinions expressed by DZone contributors are their own.
Comments