Lookup list of Maps variable in data weave script - mule

I have a list of maps (listOfMapsObject) like below
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
I would like access "Value" field for a given Id in dataweave script.
For example: For Id = 1234, Text1 should be returned.
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map {
"key" : $.key,
"data" : lookup Value field in listOfMapsObject by given key
}

Approch suggested by #'sulthony h' is fine but it will end up in performance issue if you large number of data in pyload and listOfMapsObject. As filter is used , for each record of payload script will loop for all the entries in flowVars.listOfMaps.
Following will work fine and map key value only once.
%dw 1.0
%output application/json
%var dataLookup = {(flowVars.listOfMaps map {
($.Id): $.Value
})}
---
payload map {
key : $.key,
data : dataLookup[$.key]
}
Output-
[
{
"key": "1234",
"data": "Text1"
},
{
"key": "1235",
"data": "Text2"
}
]
Where Payload -
[
{
"key" : "1234"
},
{
"key" : "1235"
}
]
And -
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
Hope this helps.

I create my own object with the slightly similar object and successfully access "value" field with the following DataWeave expression:
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map using(data = $) {
"key" : data.key,
"data" : (listOfMapsObject filter $.id == data.key).value reduce ($$ ++ $)
}
You can modify it with your own object, e.g.: replace the "id" with "Id". Test and evaluate the result by using filter, flatten, reduce, etc.

Related

How convert json object to lower case in dataweave 1.0?

Hello I have a json file as payload and strugling to do Transformation to lower case of elements and they values.
{
"Name" : "John".
"e-mails" : ['Email1#mail.com','email2#Gmail.com']
}
if its no array in values then this one works fine like where
but how to deal with arrays?
expected output:
{
"name" : "john".
"e-mails" : ['email1#mail.com','email2#gmail.com']
}
any advice?
You need to use a recursive function to cover for the other types.
%dw 1.0
%output application/json
%function lowerAll(x)
x match {
:object -> $ mapObject {
(lower $$): lowerAll($) // assumes all keys are strings
},
:array -> $ map lowerAll($),
:string -> lower $,
default -> $
}
---
lowerAll(payload)
Input:
{
"Name" : "John",
"e-mails" : ["E1mail1#mail.com","email2#Gmail.com"]
}
Output:
{
"name": "john",
"e-mails": [
"e1mail1#mail.com",
"email2#gmail.com"
]
}

Dataweave in Mule - Change value Array of Objects

I get a payload as input in the message transform component. It is an Array with Objcts:
[
{
"enterprise": "Samsung",
"description": "This is the Samsung enterprise",
},
{
"enterprise": "Apple",
"description": "This is the Apple enterprise ",
}
]
I have a variable that replaces the description and the output that I want is:
[
{
"enterprise": "Samsung",
"description": "This is the var value",
},
{
"enterprise": "Apple",
"description": "This is the var value",
}
]
I tried to use:
%dw 2.0
output application/java
---
payload map ((item, index) -> {
description: vars.descriptionValue
})
But it returns:
[
{
"description": "This is the var value",
},
{
"description": "This is the var value",
}
]
Is possible to replace only the description value keeping the rest of the fields? Avoiding adding the other fields in the mapping.
There are many ways to do this.
One way to do it is to first remove the original description field and then add the new one
%dw 2.0
output application/java
---
payload map ((item, index) ->
item - "description" ++ {description: vars.descriptionValue}
)
Otherwise you can use mapObject to iterate over the key-value pairs of each object and with pattern matching add a case for when the key is description.
I prefer this second way of doing it when I want to do many replacements.
%dw 2.0
output application/java
fun process(obj: Object) = obj mapObject ((value, key) -> {
(key): key match {
case "description" -> vars.descriptionValue
else -> value
}
})
---
payload map ((item, index) ->
process(item)
)

Replacing Null with Blank blank in Dataweave 2.0

I have to replace null with blanks in dataweave 2.0 , I tried many combinations but getting error in it .
The screenshot is attached for your reference .
Please provide any pointers for the same .
Thanks.
Its because you are assigning the blank string to dValue.doctorId and not (doctorId). Also using default is easier here for setting default values. Here is an example:
%dw 2.0
output application/xml
var doctorInformationList=[{doctorId: '1'},{doctorId: '2'}, {}]
---
root: {
DoctorInformationList: doctorInformationList map ((dValue, dIndex) ->
doctorId : dValue.doctorId default ""
)
}
It's better to use when - otherwise.
Below is dataweave transform for your problem
%dw 2.0
%output application/json
%var doctorInfoList=[{doctorId: '1', doctorName : 'A'},{doctorId: '2', doctorName : 'B'},
{doctorId: null, doctorName : 'C'},{doctorId: '', doctorName : 'D'},{}]
---
{
DoctorInfoList: doctorInfoList map ((doctorValue, doctorIndex) -> {
"docorId" : '' when doctorValue.doctorId is :null otherwise doctorValue.doctorId,
"docorName" : doctorValue.doctorName
}
)
}
Output would be like :
{
"DoctorInfoList": [
{
"docorId": "1",
"docorName": "A"
},
{
"docorId": "2",
"docorName": "B"
},
{
"docorId": "",
"docorName": "C"
},
{
"docorId": "",
"docorName": "D"
},
{
"docorId": "",
"docorName": null
}
]
}
Replace doctorInfoList with your payload

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)
}

List of map to json using Dataweave

How can i convert list of map to json using dataweave for the below example.
Input is list of map. For example, it has 3 elements id, value1, value2
[
{
"id" : "123",
"value1" : "678"
"value2" : "900"
},
{
"id" : "456",
"value1" : "679",
"value2" : "901"
}
]
Expected output:
Root element is data, value of id (123) is the key and value2, value 3 are elements in the object.
{
"data" : {
"123" : {
"value1" : "678",
"value2" : "900"
},
"456" : {
"value1" : "679",
"value2" : "901"
}
}
}
I tried to implement dataweave script but its not giving the exact output i was looking for. How can i write dataweave script to achieve this?
data: ( payload.*id map '$':payload[$$] - "id") reduce ($$ ++ $)
Should do the trick.
Try the below script
data: {
(payload map {
($.id as :string): $ -- $.id
})
}
Here, id is used as key and from value we remove the corresponding id