Reverse ordering in Mule4 - mule

I have a data in similar faashion as given in the dwl script variable.
%dw 2.0
output application/json
var test = { "2022-10-19T10:59:00.000Z":[{"kio":"spotage"}] ,
"2022-10-17T10:59:00.000Z": [{"kio":"spotage"}] ,
"2022-10-18T10:59:00.000Z": [{"kio":"spotage"}]
}
---
test orderBy $$
Need to sort by dateformat in ASC order. Getting the below response which is the default ordering DSC. Wanting to reverse it ASC. Tried -$$ and also [-1 to 0] and tried formatting as LocalDateTime check to see if it is working.
{
"2022-10-17T10:59:00.000Z": [
{
"kio": "spotage"
}
],
"2022-10-18T10:59:00.000Z": [
{
"kio": "spotage"
}
],
"2022-10-19T10:59:00.000Z": [
{
"kio": "spotage"
}
]
}
Expecting response as below
{
"2022-10-19T10:59:00.000Z": [
{
"kio": "spotage"
}
],
"2022-10-18T10:59:00.000Z": [
{
"kio": "spotage"
}
],
"2022-10-17T10:59:00.000Z": [
{
"kio": "spotage"
}
]
}
Please let me know if the question is not clear. Any thoughts?

The type of $$ is Key. Therefore you can not do -$$ directly.
You need to convert the keys to String and then to DateTime so that it can properly sort it properly.
%dw 2.0
output application/json
var test = { "2022-10-19T10:59:00.000Z":[{"kio":"spotage"}] ,
"2022-10-17T10:59:00.000Z": [{"kio":"spotage"}] ,
"2022-10-18T10:59:00.000Z": [{"kio":"spotage"}]
}
---
test orderBy -($$ as String as DateTime)

Related

How to iterate over the string using data weave 1.0 and data weave 2.0?

I'm new to dataweave and trying to transform the array and iterate over the "||" values
Input:
[
{
"card":"VISA$$0.0||MASTER$$140.0"
},
{
"card":"VISA$$0.0||MASTER$$147.0"
}
]
The DataWeave code that I tried:
%dw 2.0
output application/json
---
"CardList":payload map (data,index) ->
{
(data.card splitBy "||" map {
"sur": $
})
}
Expected response :
{
"cardList": [
{
"card": "VISA$$0.0"
},
{
"card": "MASTER$$140.0"
},
{
"card": "VISA$$0.0"
},
{
"card": "MASTER$$147.0"
}
]
}
Someone one could you please assist me here on mule 3 and 4.
thanks in advance.
Try as below - iterating through splitBy values (||)
%dw 2.0
output application/json
---
"CardList": flatten(payload map
( ($.card splitBy "||") map(item,index) ->
{
card : item
}))

Convert properties from properties file into json in Dataweave 2.0

How to convert properties from a properties file
creditmaster.metadata.AverageFicoScore=700
creditmaster.a.b.c=xyz
into this json format in a generic way
{
creditmasterMetaData: [
{
attributeKey: "AverageFicoScore",
attributeValue: 700
}
]
}
This script is generic in that it doesn't matter what are the parts of the key, it only groups by the first element (before of the first dot) and the key name after the last dot, it ignores everything in the middle:
%dw 2.3
output application/java
import * from dw::core::Strings
fun mapProperties(props) =
entriesOf(props) // since Mule 4.3 / DW 2.3
filter (substringAfter($.key, ".") startsWith "metadata.") // to filter keys with .metadata.
groupBy ((item, index) -> substringBefore(item.key, "."))
mapObject ((value, key, index) ->
(key): value map {
attributeKey: substringAfterLast($.key, "."),
attributeValue: if (isInteger($.value)) $.value as Number else $.value
}
)
---
mapProperties(payload)
Input file:
creditmaster.metadata.AverageFicoScore= 700
other.a.b= 123
creditmaster.a.b.c=xyz
something.metadata.another.maximum=456
creditmaster.metadata.different.minimum=500
Output (in JSON for clarity):
{
"something": [
{
"attributeKey": "maximum",
"attributeValue": "456"
}
],
"creditmaster": [
{
"attributeKey": "minimum",
"attributeValue": "500"
},
{
"attributeKey": "AverageFicoScore",
"attributeValue": "700"
}
]
}
One alternative is using the pluck function. It lets you iterate over an object receiving the entries.
If you have this input
{
"creditmaster": {
"metadata": {
"AverageFicoScore": "700",
"OtherData": "Some value"
}
}
}
with this transformation
{
creditmasterMetaData:
payload.creditmaster.metadata pluck ((value, key, index) ->
{
attributeKey: key,
attributeValue: value
}
)
}
you get this output
{
"creditmasterMetaData": [
{
"attributeKey": "AverageFicoScore",
"attributeValue": "700"
},
{
"attributeKey": "OtherData",
"attributeValue": "Some value"
}
]
}

Mule Dataweave Replace Null with Specific Value

My request payload looks something like below:
{
"AllCodes": [
{
"Code1": "ABC"
},
{
"Code2": "TUV"
},
{
"Code3": "XYZ"
}
]
}
I have a function which will replace the values in the above with a certain value. The code that I've now is below:
%dw 1.0
%output application/json
%function LookUp(codes) codes mapObject {($$): $.code} //function to return string values for above codes
%var codeLookup = LookUp(sessionVars.OutputCodes)
%var FinalCodes = payload.AllCodes map codeLookup[$] joinBy ';'
--
FinalCodes
The output of sessionVars.OutputCodes is:
{
"CodeMaster": {
"PrimaryCodes": {
"PrimarySpecCodes": {
"ABC": {
"code": "ABC-String1"
},
"TUV": {
"code": "TUV-String2"
}
}
}
}
}
The output that I am expecting is:
"ABC-String1;XYZ-String2;XYZ"
As you can see above, since the function is returning the values for only "ABC" and "TUV" codes, my final output should have the original value if no function value found.
I was trying to use a default value before map operator. But it doesn't seem to work.
We get all the values for allCodes with $ pluck $, then if codes[$] is not found, we default to the value $. I believe you just need to add default $ to your original dataweave for it to work, but I gave a complete solution for other users on StackOverflow.
%dw 1.0
%output application/json
%var outputCodes =
{
"CodeMaster": {
"PrimaryCodes": {
"PrimarySpecCodes": {
"ABC": {
"code": "ABC-String1"
},
"TUV": {
"code": "TUV-String2"
}
}
}
}
}
%var allCodes =
{
"AllCodes": [
{
"Code1": "ABC"
},
{
"Code2": "TUV"
},
{
"Code3": "XYZ"
}
]
}
%var codes = outputCodes.CodeMaster.PrimaryCodes.PrimarySpecCodes mapObject {
($$): $.code
}
---
(flatten (allCodes.AllCodes map ($ pluck $))) map (
codes[$] default $
) joinBy ';'
this produces:
"ABC-String1;TUV-String2;XYZ"
There are various ways to solve this. One could be as follows:
%dw 1.0
%output application/json
%var payload2= { "CodeMaster": { "PrimaryCodes": { "PrimarySpecCodes": { "ABC": {"code": "ABC-String1"}, "TUV": {"code": "TUV-String2"} } } } }
%var codes = payload2.CodeMaster.PrimaryCodes.PrimarySpecCodes mapObject {
($$) : $.code
}
%var finalCodes = (payload.AllCodes map {
a: $ mapObject {
a1: codes[$] default $
}
}.a.a1) joinBy ";"
---
finalCodes

How to only include a key if the value meets a certain criteria

I have this piece of Dataweave code
list_of_orders: {
order: payload map ((payload01 , indexOfPayload01) -> {
order_dtl:
"" when payload01[30] == "S"
otherwise
"" when payload01[30] == "C"
otherwise
[{
data: some_data
}],
order_hdr: {
data: some_data
}
})
}
This code will output the following data
"list_of_orders": {
"order": [
{
"order_dtl": [
{
"data": "some_data"
}
],
"order_hdr": {
"data": "some_data"
}
}
]
}
But it will only do this if payload01[30] != "S" or "C"
If payload01[30] is equal to "S" or "C" then it does this
"list_of_orders": {
"order": [
{
"order_dtl": "",
"order_hdr": {
"data": "some_data"
}
}
]
}
The reason I have done this is because I have been asked to only include the DETAIL line if the order_type is not "C" or "S".
The problem is that the actual key - order_dtl - is still present and I don't want anything there at all.
How do I make a KEY conditional?
Any help appreciated
Thanks
What you are looking is called conditional elements
list_of_orders: {
order: payload map ((payload01 , indexOfPayload01) -> {
(order_dtl:
[{
data: some_data
}]) when((payload01[30] != "S") and (payload01[30] != "C")),
order_hdr: {
data: some_data
}
})
}

How can I transform an array of objects to an array of strings and not lose the key in Dataweave?

Hi I need to transform the following JSON object:
{
"products": [
{
"itemno": "123131",
"description" : "Big Widget",
"attributes": [
{
"color": [
{
"value": "Red",
"codeValue": "RED_NO2"
},
{
"value": "Blue Licorice",
"codeValue": "BLUE-355"
}
]
},
{
"chemicals": [
{
"value": "Red Phosphorous",
"codeValue": "RED_PHOS"
},
{
"value": "Chlorine Bleach",
"codeValue": "CHLRN_BLCH"
}
]
}
]
}
]
}
I am trying to transform this with each attribute having an array of values where their value is the codeValue and it's an array of those string values.
This is the desired output:
{
"products": [
{
"itemno": "123131",
"description: : "Big Widget",
"attributes": [
{
"color": ["RED_NO2", "BLUE-355"]
},
{
"chemicals": ["RED_PHOS", "CHLRN_BLCH"]
}
]
}
]
}
This is the Dataweave. I cannot determine how to get the attribute names (i.e. color, chemicals as keys with the desired data.
There is not a lot of good examples on transforming data with Dataweave and I have spent a lot of time trying to figure this out.
This is one of the dataweaves that got there somewhat, but isn't the solution:
%dw 1.0
%output application/json
---
payload.products map
{
"ItemNo" : $.sku,
"Desc" : $.description,
"Test" : "Value",
"Attributes" : $.attributes map
{
'$$' : $ pluck $.value
}
}
Your help is greatly appreciated.
You can do something like this:
%dw 1.0
%output application/json
%function attributeCodeValues(attributes)
attributes map ((attr) ->
attr mapObject ((values, descriptor) ->
{
(descriptor): values map $.codeValue
}
)
)
---
payload.products map {
"ItemNo" : $.sku,
"Desc" : $.description,
"Test" : "Value",
"Attributes" : attributeCodeValues($.attributes)
}