SlideShare a Scribd company logo
Mule Concepts
DataWeave VII
• Pattern matching executes on the first pattern that
matches the specified expression.
• DataWeave supports four different types of patterns:
– Literal
– type/traits
– Regex
– Expression
• Each pattern type can be either named or
unnamed.
DataWeave – Body - Pattern matching
• The example below is a model for how matching
works :
---
value match {
(<name>:)?<pattern> -> <when matched>,
(<name>:)?<pattern> -> <when matched>,
default -> <when none of them matched>
}
---
DataWeave – Body - Pattern matching
• Literal Pattern :
– Matches when the evaluated value equals a simple literal value.
– Example :
– In this example, the first field simply matches the value in
'payload.string' and returns a boolean, the second field performs the
same match, but returns an object that contains both a boolean and a
reference to the validated value.
DataWeave – Body - Pattern matching
Transform Input Output
%dw 1.0
%output application/json
---
a: payload.string match {
"Emiliano" -> true,
"Mariano" -> false
},
b: payload.string match {
str: "Emiliano" -> { "matches": true, value: str },
str: "Mariano" -> { "matches": false, value: str }
}
{ "string": "Emiliano" }
{
"a": true,
"b": {
"matches": true,
"value": "Emiliano"
}
}
• Expression Pattern :
– Matches when running a certain expression over the evaluated value
returns true.
– Example :
– In this example, the first field matches the value of 'payload.string' against two
alternatives and conditionally appends a different string to it; the second field
evaluates if the value in 'payload.number' is larger or smaller than 3 and
returns "lower" or "higher" accordingly.
DataWeave – Body - Pattern matching
Transform Input Output
%dw 1.0
%output application/json
---
{
a: payload.string match {
str when str == "Mariano" -> str ++ " de Achaval",
str when str == "Emiliano" -> str ++ " Lesende" },
b: payload.number match {
n when n < 3 -> "lower",
n when n > 3 -> "higher" }}
{
"string":
"Emiliano",
"number": 3.14
}
{
"a": "Emiliano
Lesende",
"b": "higher"
}
• Match Type :
– Matches when the evaluated value is of the specified type.
– Example :
DataWeave – Body - Pattern matching
Transform Input Output
%dw 1.0
%output application/json
---
{
a: payload.a match {
:object -> "OBJECT",
:string -> "STRING",
:number -> "NUMBER",
:boolean -> "BOOLEAN",
:array -> "ARRAY",
:null -> "NULL"
},
b: payload.b match {
y is :object -> { type: "OBJECT", y: y },
y is :string -> { type: "STRING", y: y },
y is :number -> { type: "NUMBER", y: y },
y is :boolean -> { type: "BOOLEAN", y: y },
y is :array -> { type: "ARRAY", y: y },
y is :null -> { type: "NULL", y: y }
}}
{
"a": "Emiliano",
"b": 3.14
}
{
"a": "STRING",
"b": {
"type": "NUMBER",
"y": 3.14
}
}
• Match Type :
– In this example, the first field evaluates the type of 'payload.a' and returns a
different string with the type name depending on what type it matches with
(this could be easier done through the Type Of operator), the second field
returns an object with the same type name as a string and a reference to the
evaluated value.
DataWeave – Body - Pattern matching
• Match Regex :
– Matches when the evaluated value fits a given regular expression.
– Example :
DataWeave – Body - Pattern matching
Transform Input Output
%dw 1.0
%output application/json
---
{
a: payload.phones map ($ match {
/+(d+)s((d+))s(d+-d+)/ -> { country: $[0], area:
$[1], number: $[2] },
/((d+))s(d+-d+)/ -> { area: $[1], number: $[2] }
}),
b: payload.phones map ($ match {
phone: /+(d+)s((d+))s(d+-d+)/ -> { country:
phone[0], area: phone[1], number: phone[2] },
phone: /((d+))s(d+-d+)/ -> { area: phone[1],
number: phone[2] }
})
}
{
"phones": [
"+1 (415) 229-
2009",
"(647) 456-
7008"
]
}
{
"a": [
{
"country": "+1 (415) 229-
2009","area": "1","number":
"415"
},{
"area": "647",
"number": "456-7008"
} ],
"b": [
{"country": "+1 (415) 229-
2009",
"area": "1",
"number": "415"},
{"area": "647",
"number": "456-7008" }]}
• Match Regex :
– In this example, the payload includes two elements in an array, and in both
cases the Map operatorto cycle through the array. It then evaluates each
element agains a regular expression and outputs a different object depending
on what kind of match is found.
DataWeave – Body - Pattern matching

More Related Content

PPTX
Mule data weave_6
PPTX
Mule data weave_5
PPTX
Mule data weave_3
PDF
MuleSoft DataWeave data transformation language
PPTX
Data weave documentation
PPTX
Mule data weave_8
PPT
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
PPTX
Variables in dataweave
Mule data weave_6
Mule data weave_5
Mule data weave_3
MuleSoft DataWeave data transformation language
Data weave documentation
Mule data weave_8
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
Variables in dataweave

Viewers also liked (8)

PPTX
Mule data weave_10
PPTX
Mule scopes async_scope
PPTX
Mule mel 3
PPTX
Mule mel 5_tips
PPTX
Mule data weave_4
PPTX
Mule data weave_2
PDF
10 Useful Asciidoctor Tips
PPT
Mule soft mel_session_3
Mule data weave_10
Mule scopes async_scope
Mule mel 3
Mule mel 5_tips
Mule data weave_4
Mule data weave_2
10 Useful Asciidoctor Tips
Mule soft mel_session_3
Ad

Similar to Mule data weave_7 (20)

PDF
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
PPTX
Data weave (MuleSoft)
PDF
MuleSoft Nashik Virtual Meetup#3 - Deep Dive Into DataWeave and its Module
PPTX
Data weave more operations
PPTX
Mule soft meetup_charlotte_4__draft_v2.0
PPTX
Data weave 2.0 advanced (recursion, pattern matching)
PPTX
Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
PDF
Configurable Pattern Matching Semantics in openCypher: Defining Levels of Nod...
PDF
Pune Mule Meetups July 2019
PPTX
Easy Dataweave transformations - Ashutosh
PPTX
Vancouver Canada MuleSoft Meetup Nov 2020
PPTX
New York City Meetup- 6th March 2021
PPTX
Indianapolis mule soft_meetup_14_apr_2021-Review a complex Dataweave Transfor...
PPTX
Mule soft meetup_virtual_ charlotte_2020_final1
PPTX
Power of Transformation with DataWeave 2.X Engine
PPTX
Data weave 2.0 language fundamentals
PDF
Structural Pattern Matching in Python
PDF
ECMA 入门
PPTX
Mulesoftmeetup4th july
PDF
Json the-x-in-ajax1588
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Data weave (MuleSoft)
MuleSoft Nashik Virtual Meetup#3 - Deep Dive Into DataWeave and its Module
Data weave more operations
Mule soft meetup_charlotte_4__draft_v2.0
Data weave 2.0 advanced (recursion, pattern matching)
Toronto Virtual Meetup #11 - Reviewing Complex DataWeave Transformation Use-case
Configurable Pattern Matching Semantics in openCypher: Defining Levels of Nod...
Pune Mule Meetups July 2019
Easy Dataweave transformations - Ashutosh
Vancouver Canada MuleSoft Meetup Nov 2020
New York City Meetup- 6th March 2021
Indianapolis mule soft_meetup_14_apr_2021-Review a complex Dataweave Transfor...
Mule soft meetup_virtual_ charlotte_2020_final1
Power of Transformation with DataWeave 2.X Engine
Data weave 2.0 language fundamentals
Structural Pattern Matching in Python
ECMA 入门
Mulesoftmeetup4th july
Json the-x-in-ajax1588
Ad

More from kunal vishe (20)

PPTX
Mule mel 4_tips
PPTX
Mule mel 2
PPTX
Mule mel 1
PPTX
Mule scopes request_response_scope
PPTX
Mule scopes foreach_scope
PPTX
Mule scopes 2
PPTX
Mule scopes 1
PPTX
Mule debugging logging_configuration_in_mule
PPTX
Mule debugging logging_in_mule
PPTX
Mule debugging managing_stack_traces
PPTX
Mule error handling_rollback exception strategy
PPTX
Mule message
PPTX
Mule error handling_choice exception strategy
PPTX
Mule concepts exception strategies
PPTX
Mule concepts filters scopes_routers
PPTX
Mule concepts transformers
PPTX
Mule concepts components
PPTX
Mule concepts connectors
PPTX
Mule concepts elements in a mule flow
PPTX
Mule concepts flows
Mule mel 4_tips
Mule mel 2
Mule mel 1
Mule scopes request_response_scope
Mule scopes foreach_scope
Mule scopes 2
Mule scopes 1
Mule debugging logging_configuration_in_mule
Mule debugging logging_in_mule
Mule debugging managing_stack_traces
Mule error handling_rollback exception strategy
Mule message
Mule error handling_choice exception strategy
Mule concepts exception strategies
Mule concepts filters scopes_routers
Mule concepts transformers
Mule concepts components
Mule concepts connectors
Mule concepts elements in a mule flow
Mule concepts flows

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Transform Your Business with a Software ERP System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administraation Chapter 3
PPT
Introduction Database Management System for Course Database
PPTX
history of c programming in notes for students .pptx
PDF
Designing Intelligence for the Shop Floor.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How to Migrate SBCGlobal Email to Yahoo Easily
Design an Analysis of Algorithms I-SECS-1021-03
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Systems & Binary Numbers (comprehensive )
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Design an Analysis of Algorithms II-SECS-1021-03
CHAPTER 2 - PM Management and IT Context
Transform Your Business with a Software ERP System
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administraation Chapter 3
Introduction Database Management System for Course Database
history of c programming in notes for students .pptx
Designing Intelligence for the Shop Floor.pdf

Mule data weave_7

  • 2. • Pattern matching executes on the first pattern that matches the specified expression. • DataWeave supports four different types of patterns: – Literal – type/traits – Regex – Expression • Each pattern type can be either named or unnamed. DataWeave – Body - Pattern matching
  • 3. • The example below is a model for how matching works : --- value match { (<name>:)?<pattern> -> <when matched>, (<name>:)?<pattern> -> <when matched>, default -> <when none of them matched> } --- DataWeave – Body - Pattern matching
  • 4. • Literal Pattern : – Matches when the evaluated value equals a simple literal value. – Example : – In this example, the first field simply matches the value in 'payload.string' and returns a boolean, the second field performs the same match, but returns an object that contains both a boolean and a reference to the validated value. DataWeave – Body - Pattern matching Transform Input Output %dw 1.0 %output application/json --- a: payload.string match { "Emiliano" -> true, "Mariano" -> false }, b: payload.string match { str: "Emiliano" -> { "matches": true, value: str }, str: "Mariano" -> { "matches": false, value: str } } { "string": "Emiliano" } { "a": true, "b": { "matches": true, "value": "Emiliano" } }
  • 5. • Expression Pattern : – Matches when running a certain expression over the evaluated value returns true. – Example : – In this example, the first field matches the value of 'payload.string' against two alternatives and conditionally appends a different string to it; the second field evaluates if the value in 'payload.number' is larger or smaller than 3 and returns "lower" or "higher" accordingly. DataWeave – Body - Pattern matching Transform Input Output %dw 1.0 %output application/json --- { a: payload.string match { str when str == "Mariano" -> str ++ " de Achaval", str when str == "Emiliano" -> str ++ " Lesende" }, b: payload.number match { n when n < 3 -> "lower", n when n > 3 -> "higher" }} { "string": "Emiliano", "number": 3.14 } { "a": "Emiliano Lesende", "b": "higher" }
  • 6. • Match Type : – Matches when the evaluated value is of the specified type. – Example : DataWeave – Body - Pattern matching Transform Input Output %dw 1.0 %output application/json --- { a: payload.a match { :object -> "OBJECT", :string -> "STRING", :number -> "NUMBER", :boolean -> "BOOLEAN", :array -> "ARRAY", :null -> "NULL" }, b: payload.b match { y is :object -> { type: "OBJECT", y: y }, y is :string -> { type: "STRING", y: y }, y is :number -> { type: "NUMBER", y: y }, y is :boolean -> { type: "BOOLEAN", y: y }, y is :array -> { type: "ARRAY", y: y }, y is :null -> { type: "NULL", y: y } }} { "a": "Emiliano", "b": 3.14 } { "a": "STRING", "b": { "type": "NUMBER", "y": 3.14 } }
  • 7. • Match Type : – In this example, the first field evaluates the type of 'payload.a' and returns a different string with the type name depending on what type it matches with (this could be easier done through the Type Of operator), the second field returns an object with the same type name as a string and a reference to the evaluated value. DataWeave – Body - Pattern matching
  • 8. • Match Regex : – Matches when the evaluated value fits a given regular expression. – Example : DataWeave – Body - Pattern matching Transform Input Output %dw 1.0 %output application/json --- { a: payload.phones map ($ match { /+(d+)s((d+))s(d+-d+)/ -> { country: $[0], area: $[1], number: $[2] }, /((d+))s(d+-d+)/ -> { area: $[1], number: $[2] } }), b: payload.phones map ($ match { phone: /+(d+)s((d+))s(d+-d+)/ -> { country: phone[0], area: phone[1], number: phone[2] }, phone: /((d+))s(d+-d+)/ -> { area: phone[1], number: phone[2] } }) } { "phones": [ "+1 (415) 229- 2009", "(647) 456- 7008" ] } { "a": [ { "country": "+1 (415) 229- 2009","area": "1","number": "415" },{ "area": "647", "number": "456-7008" } ], "b": [ {"country": "+1 (415) 229- 2009", "area": "1", "number": "415"}, {"area": "647", "number": "456-7008" }]}
  • 9. • Match Regex : – In this example, the payload includes two elements in an array, and in both cases the Map operatorto cycle through the array. It then evaluates each element agains a regular expression and outputs a different object depending on what kind of match is found. DataWeave – Body - Pattern matching