02-06-2024 09:32 AM
Hello, I have the following MuleSoft code im trying to convert into SnapLogic. I have tried everything i can think of and im not getting anywhere. Also, posted below is a sample output from stored procedure and the expected output i need. Thank you for you time and help!
MuleSoft dataWeave Code:
%dw 2.0
output application/json
var payloadGroupByRiskLevel = ((vars.edpResponse.resultSet1 map ((item, index) -> {
"state": item.SupplierLocationStateCode default "No_State_Code",
"country": item.SupplierLocationCode,
"riskLevel": item.RiskLevel
})) groupBy ((item, index) -> item.riskLevel))
var overallRisk = avg((vars.edpResponse.resultSet1 map ((item, index) -> item.AggregateRisk) default [])) as String {format: "0.00"}
---
{
"riskDetails": {
"riskScore": {
"overAllRisk": overallRisk,
"lowRiskCount": sizeOf(payloadGroupByRiskLevel["Low Risk"] default []),
"mediumRiskCount": sizeOf(payloadGroupByRiskLevel["Medium Risk"] default []),
"highRiskCount": sizeOf(payloadGroupByRiskLevel["High Risk"] default []),
"statesByLowRisk": ((payloadGroupByRiskLevel["Low Risk"].state) distinctBy $) orderBy $,
"statesByMediumRisk": ((payloadGroupByRiskLevel["Medium Risk"].state) distinctBy $) orderBy $,
"statesByHighRisk": ((payloadGroupByRiskLevel["High Risk"].state) distinctBy $) orderBy $,
"countriesByLowRisk": ((payloadGroupByRiskLevel["Low Risk"].country) distinctBy $) orderBy $,
"countriesByMediumRisk": ((payloadGroupByRiskLevel["Medium Risk"].country) distinctBy $) orderBy $,
"countriesByHighRisk": ((payloadGroupByRiskLevel["High Risk"].country) distinctBy $) orderBy $,
}
}
}
Output from stored Prodcedure:
{
"riskDetails": {
"riskScore": {
"overAllRisk": “79.83”
"lowRiskCount": 1,
"mediumRiskCount": 0,
"highRiskCount": 2,
"statesByLowRisk": ["No_State_Code"],
"statesByMediumRisk": [],
"statesByHighRisk": ["Minnesota"],
"countriesByLowRisk": ["SWEDEN"],
"countriesByMediumRisk": [],
"countriesByHighRisk": ["AUSTRALIA","UNITED STATES"]
}
}
}
Solved! Go to Solution.
02-08-2024 05:25 AM
@Max - as usual, different developers approach a solution differently. Here is my take on it.
It took me a moment to realize I had to first deduplicate the dataset, which is what the Map for Unique, Sort for Unique, and Unique snaps are doing. Then it's a pretty simple Aggregate using some conditional logic in the expressions, the the final Mapper to get everything aligned as desired.
Note the use of nested ternary in the "Map final" to handle the No_State_Code default values. I typically don't prefer to use nested ternary as it gets difficult to interpret pretty quickly... normally I would use the Conditional snap to handle these cases.
Pipeline is attached.
Hope this helps!
02-07-2024 12:18 PM - edited 02-07-2024 12:19 PM
Hello, wondering if you might have a look at this issue. I appreciate your help. Thank you!
02-07-2024 04:46 PM - edited 02-07-2024 04:56 PM
Hi Max,
There may be a few more tweaks needed depending on how you would handle any unexpected data, but the below pipeline should give you a good start. You could cut down the number of Snaps in the pipeline with Script Snaps or additional expressions in the Mapper Snaps.
02-08-2024 05:25 AM
@Max - as usual, different developers approach a solution differently. Here is my take on it.
It took me a moment to realize I had to first deduplicate the dataset, which is what the Map for Unique, Sort for Unique, and Unique snaps are doing. Then it's a pretty simple Aggregate using some conditional logic in the expressions, the the final Mapper to get everything aligned as desired.
Note the use of nested ternary in the "Map final" to handle the No_State_Code default values. I typically don't prefer to use nested ternary as it gets difficult to interpret pretty quickly... normally I would use the Conditional snap to handle these cases.
Pipeline is attached.
Hope this helps!
02-08-2024 01:16 PM
@koryknickThis is perfect, Thank you!