Grouping and Sorting of records in Kumologica

Image by Pexels from Pixabay

Grouping and sorting of records are common functionality that are seen in many microservice as well as integration requirements. In most of the use cases the records fetched from the source system might be in raw form having all the records treated seperate without having any records grouped as per the functional requirement. 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 the records of 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 as keys. In the below diagram we can see cars of different brands are grouped based on the their color and type property. The group 1 has both Ford and BMW since they have the same traits of color and type.

Grouping of cars based on color and type

Sorting

Sorting is the functionality of ordering the records based on a property associated with the record. In the below diagram we can see cars of different brands are stacked based on the RegNo order.

cars ordering based on the ReNo

How we do this in Kumologica?

In this article we are going to show you how grouping and ordering of records can be achieved in Kumologica. If you are new to Kumologica we would recommend to go through our articles and youtube videos to get an insight. Kumologica is one of the early players in this space which brings the benefits of the low-code integration capability of traditional integration to the new world of serverless.

Kumologica provided two major means to implementing the grouping and sorting of records. You may either use datamapper node or use function node to achieve this. In this article we will focus only on datamapper node as in function node you can device your own javascript logic to achieve the grouping or ordering.

Grouping of records using datamapper node

Lets assume that the incoming message payload is having the following JSON content. We will group the JSON objects (individual cars) based on the Color and Type.

{
"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.

msg.payload.Cars{
Color & Type:[
$.{
"Car" : $.Car,
"Color" : $.Color,
"Type" : $.Type
}]
}

Following will be the response after grouping

{
"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 of records using datamapper node

Lets assume that the incoming message payload is having the following JSON payload.

{
"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 with in the datamapper node.

msg.payload.Cars^(RegNo).{
"Car" : $.Car,
"Color" : $.Color,
"RegNo" : $.RegNo
}

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"
}
]

Conclusion

This article has shown how easy to implement the grouping and sorting logic in Kumologica using the datamapper node.

Remember Kumologica is totally free to download and use. Go ahead and give it a try, we would love to hear your feedback.

--

--

--

Kumologica is the first low-code development solution that makes your integration services run on serverless compute regardless the cloud provider.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Ergo Hackathon: Join Us On Discord

Blazor — The Third Time’s the Charm from Microsoft

Be Functional With Java — get rid of if/else null checks with Vavr’s Option

How to Retrieve MySQL Last Insert ID in CodeIgniter 4

Consider a scenario where your Mobile Application needs a Firebase push notification with action…

[Leet Code] Relative Ranks

Patch Notes: January 2020

Or class power as.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kumologica

Kumologica

Kumologica is the first low-code development solution that makes your integration services run on serverless compute regardless the cloud provider.

More from Medium

AWS Serverless : A New Way to Manage Your WebApps

What is Serverless Architecture? And Why Businesses Are Going Serverless

Why software architecture matters to you and your customers

I spoke at the Serverless Stack 1.0 Conference today!