https://www.caeliusconsulting.com/blogs/transforming-messages-with-dataweave/
- Add 1 to each value in the array
[1,2,3,4,5]
Ans:
%dw 2.0
output application/json
---payload map $+1
2)Get a list ofids from:
[ { "id": 1, "name": "Archer" },
{ "id": 2, "name": "Cyril" },
{ "id": 3, "name": "Pam" } ]%dw 2.0output application/json---payload map $.id
___________________________________________________________________________________
Example 1: Using the input data, we'll produce a patterned output based on the given array.
Input:
[
{"name": "Roger" },
{"name": "Michael" },
{"name": "Harris" }
]Output:
[
{
"user 1": "Roger"
},
{
"user 2": "Micheal"
},
{
"user 3": "Harris"
}
]Dataweave code:
Solution1: without map
%dw 2.0output application/json---{"user1":payload[0].name,"user2":payload[1].name,"user3":payload[2].name}
Solution2: with map
%dw 2.0 output application/json ---payload map((item,index)->{("user" ++" "++ index+1): item.name})____________________________________________________________________________
Example 2: Given two arrays, [1,2,3,4,5,6,7] and [4,5,6,7,1], we’ll merge into a single array without duplicates, [1,2,3,4,5,6,7]:
Dataweave Code:
%dw 2.0output application/jsonvar array1= [1,2,3,4,5,6,7,9,10] var array2= [4,5,6,7,1,10]var sum=array1 ++ array2---sum distinctBy ((item, index) -> item )
OutPut:[
1,
2,
3,
4,
5,
6,
7,
9,
10
]
____________________________________________________________________________________
Example 3: From the object, we’ll remove the “last name” and “first name” key-value pair.
Input:
{
"first name": "Keith",
"last name": "Peters",
"age": 25
}
Output:
{
"age": 25
}
Solution1: Without filterobject
%dw 2.0output application/json---payload -- payload.&"first name" -- payload.&"last name"
Solution2: With filterObject filter
%dw 2.0output application/json---payload filterObject ((value, key, index) -> (key as String != "first name" and key as String !="last name") )
Example 4: From the array [1,2,3,’a’,’b’,'c'], remove 2 and b and c .
Solution 1:%dw 2.0output application/jsonvar array1=[1, 2, 3, 'a','b', 'c']---array1 - 2 -'a' -'c'
Solution 2:
%dw 2.0output application/jsonvar array1=[1, 2, 3, 'a','b', 'c']---array1 -- [2 ,'a' ,'c']
Output is: [ 1, 3, "b"]
_____________________________________________________________________________________
Example 5: Given the below JSON, transform all names to uppercase.
Input Payload :
[
{"name": "Sravan" },
{"name": "Kumar" },
{"name": "Singamaneni" }
]
Output Pattern:
[
{
"NAME": "SRAVAN"
},
{
"NAME": "KUMAR"
},
{
"NAME": "SINGAMANENI"
}
]
Expression Used to Transform:
%dw 2.0output application/json
---payload map ((item, index) ->NAME : upper(item.name) )
______________________________________________________________________________________
Example 6: Retrieve all the records of users with the age greater than 20.
[
{
"name": "Chris Jordan",
"age": "19"
},
{
"name": "Glenn Maxwell",
"age": "24"
},
{
"name": "Mike",
"age": "20"
}
]
DWcode:
%dw 2.0output application/json
---payload filter ((item, index) -> item.age > 20)
Output:
[ { "name": "Glenn Maxwell", "age": "24" }]
__________________________________________________________________________________
Example 7: Retrieve the average age of persons in the input payload.
[
{
"name": "Sravan",
"age": "19"
},
{
"name": "kumar",
"age": "24"
},
{
"name": "singamaneni",
"age": "20"
}
]
Exercise 1:
Basic Absolute Value Transformation
Write a DataWeave script that converts a list of integers into their absolute values.
Input:
Expected Output:
Comments
Post a Comment