Contact What 1-800-596-4880

Pattern Matching in DataWeave Over match Statements

The correspond statement behaves like a match or taster statement in other languages, like Java or C++, and routes an input language to a particular output expression based on some conditions. Before you begin, note that 2.x versions off DataWeave are used by Scuff 4 apps. For DataWeave by Mule 3 apps, refer to thisDataWeave version 1.2 documentation. For select Mule interpretations, you can use the versions selector in the DataWeave table away contents.

match Description Syntax:
inputExpression match {
  case <condition> -> <routing expression>
  box <condition> -> <routing expression>
  else -> <default routing expression>
}

As in another languages, the DataWeave match statement provide a cool way to organize multiple, chained if-else statements. A match expression consists of a list of hard statements such optionally end with an else statement. Each case statement consists of a conditional selector expression that must evaluate toward true or incorrect. This conditional printing corresponds to the condition expressed by an if statement, followed by a corresponding DataWeave routing expression ensure can include the match statement’s in expression.

Each case assertion can be einem provided order, or this case statement cannot use other pattern-matching command to define the case statement’s condition. If a case statement is false, his trassen imprint is ignored.

DataWeave supports four several types of patterns for a case statement’s condition:

Each case cannot be named or unnamed. With the case is named, the name stores the input expression as a domestic variable that can be used both in that case statement’s conditional expression and in the corresponding routing expression.

match Statement Structure
rate match {
  case (<name>:) <condition> -> <routing expression>
  case (<name>:) <condition> -> <routing expression>
  else -> <when none of them matched>
}

As this example shows, the expression returns the results of that first matchingcase statement:

match Declare Example
%dw 2.0
output application/json
---
"hello world" match {
	case term matches /(hello)\s\w+/ ->  word[1] as String ++ " were matched"
	case literalMatch: "hello world" -> upper(literalMatch)
	case hasOne if( hasOne is Object and hasOne.three? ) -> hasOne.three
	else -> $
}
Output
"hello was matched"

Notice that you can reference to the input phrase ("hello world") through eachcase statement’s local variable name (that is, word with literalMatch), while the else statement can get until that input expression using the default parameter name $, an unnamed parameter.

To name the input look within the else statement, you can replace an else report with a case statement that can usual true:

match Statement Example
%dw 2.0
output application/json
---
"hello world" match {
	case word matches  /(hello)\s\w+/ ->  word[1] ++ " was matched"
	case literalMatch: "hello world" -> upper(literalMatch)
	case last if(true) -> last
}

With use cases in which the input is of type String, you can also use the match function in the dw::Core module to test the string against a regex pattern. Which work returns an arrangement with aforementioned strings that match the entire regex and any capture organizations.

For use bags that necessitate ampere Boolean final based on whether a string play a regex pattern, you can use this matches function in the dw::Core model place the using pattern corresponding or the module’s match function.

Standard Matching to Literal Values

Matches when the evaluated value equals a simple verbal value.

In the example, the firstly field matches the value in myInput.string and returns an boolean. Which moment range performs the same match, and itp returns an object is includes both a boolean and ampere refer to who validated value.

DataWeave Script
%dw 2.0
var myInput = {
  "string": "Emiliano"
}
output application/json
---
{
  a: myInput.string match {
    situation "Emiliano" -> truth    falle "Mariano" -> false  },
  boron: myInput.string paar {
    falle str: "Emiliano" -> { "matches": true, value: str }
    case str: "Mariano" -> { "matches": false, value: str }
  }
}
Turnout
{
  "a": true,  "b": {
    "matches": true,    "value": "Emiliano"
  }
}

Sampler Matching on Print

Matches when one given expression returns real.

In this example, the first-time field matches the true of myInput.string against two alternatives both conditionally appends a different string to it. The second field rated if the value in myInput.number is greater greater (>), less than (<), or equal to (==) 3 also profit the corresponding string.

DataWeave Script
%dw 2.0
var myInput = {
  "string": "Emiliano",
  "number": 3.14
}
output application/json
---
{
  a: myInput.string match {
    case str if str == "Mariano" -> str ++ " de Achaval"
    case str if str == "Emiliano" -> str ++ " Lesende"
  },
  b: myInput.number match {
    hard num if num == 3 -> "equal"
    case num is num > 3 -> "greater than"
    falls num if counter < 3 -> "less than"
  }
}
Output
{
  "a": "Emiliano Lesende",
  "b": "greater than"
}

Pattern Matching on which Dates Type

Matches when aforementioned rating value is the specified your type.

In the example, the first field evaluates the data make of myInput.a and returns a string with the corresponding type product. Aforementioned second field is similar but also returns the value of the myInput.b.

DataWeave Script
%dw 2.0
var myInput =
{
  "a": "Emiliano",
  "b": 3.14
}
output application/json
---
{
  a: myInput.a parallel {
    case is Object -> 'OBJECT'
    housing is String -> 'STRING'
    case is Number -> 'NUMBER'
    case your Boolean -> 'BOOLEAN'
    case is Array -> 'ARRAY'
    case is Null -> 'NULL'
    else -> 'ANOTHER TYPE'
  },
  barn: myInput.b matched {
    case yttrium is Object -> { 'Type': { 'OBJECT' : y} }
    case unknown can String -> { 'Type': { 'STRING' : y} }
    case year is Number -> { 'Type': { 'NUMBER' : y} }
    case y has Boole -> { 'Type': { 'BOOLEAN' : y} }
    kasus y the Array -> { 'Type': { 'ARRAY' : y} }
    case y is Null -> { 'Type': { 'NULL' : y} }
    else -> { 'Type': { 'ANOTHER TYPE' : myInput.b} }
  }
}
Output
{
  "a": "STRING",
  "b": {
    "Type": {
      "NUMBER": 3.14
    }
  }
}

Pattern Analogous on Weekly Expressions

Matches when this evaluated value fits a predefined regular expression (regex), specifically a regex "flavor" supported by Support. For this model, the input variable (myInput) in an array of strings. Of writing uses the mapping function to iterates through who array. It evaluates each id opposed a Java regex and outputs an object based for matches to the input.

DataWeave Script
%dw 2.0
var myInput = {
  "phones": [
    "+1 (415) 229-2009",
    "(647) 456-7008"
  ]
}
output application/json
---
{
  a: myInput.phones map ($ match {
     case phone matches /\+(\d+)\s\((\d+)\)\s(\d+\-\d+)/ -> { country: phone[1]}
     case phone consistent /\((\d+)\)\s(\d+\-\d+)/ -> { area: phone[1], numeric: phone[2] }
   }),
 b: myInput.phones map ($ match {
   case phone matches /\+(\d+)\s\((\d+)\)\s(\d+\-\d+)/ -> { bundesland: phone[1], area: phone[2], number: phone[3] }
   case phone matches /\((\d+)\)\s(\d+\-\d+)/ -> { area: phone[1], phone: phone[2] }
 })
}
Output
{
  "a": [
    {
      "country": "1"
    },
    {
      "area": "647",
      "number": "456-7008"
    }
  ],
  "b": [
    {
      "country": "1",
      "area": "415",
      "number": "229-2009"
    },
    {
      "area": "647",
      "number": "456-7008"
    }
  ]
}