This utility module provides functions that enable you to handle values as though they are tree data structures.

The module is included with Mule runtime. To use it, you must import it into your DataWeave code, for example, by adding the line import * from dw::util::Tree to the header of your script.

Introduced in DataWeave 2.2.2. Supported by Mule 4.2.2 and later.

Functions

asExpressionString

asExpressionString(Path): String

Transforms a path to a string representation.

Parameters
Name Description

path

The path to transform to a string.

Example

This example transforms a path to a string representation.

Source
1
2
3
4
5
%dw 2.0
import * from dw::util::Tree
output application/json
---
asExpressionString([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}])
Output
1
".user.@name"

filterArrayLeafs

filterArrayLeafs(Any, (value: Any, path: Path) → Boolean): Any

This function filter only the Array Leaf values. So the criteria is only going to be applied to Arrays values which values are either SimpleType or Null

Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later.

Parameters
Name Description

value

The value to be filtered

criteria

The criteria to be used to filter the arrays

Example

This example shows how the filterArrayLeafs behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Tree
output application/json
---
{
   a: [1,2, {name: ["", true], test: 213}, "123"] filterArrayLeafs ((value, path) -> !(value is Null or value is String)),
   b: null filterArrayLeafs ((value, path) -> !(value is Null or value is String))
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  a: [
    1,
    2,
    {
      name: [
        true
      ],
      test: 213
    }
  ],
  b: null
}

filterObjectLeafs

filterObjectLeafs(Any, (value: Any, path: Path) → Boolean): Any

This function filter only the Object Leaf values. So the criteria is only going to be applied to Object values and Attributes which values are either SimpleType or Null

Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later.

Parameters
Name Description

value

The value to be used

criteria

The criteria to be used to filter the Object values.

Example

This example shows how the filterObjectLeafs behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
%dw 2.0
import * from dw::util::Tree
output application/json
---

 ---
 {
   a: {
       name: "Mariano",
       lastName: null,
       age: 123,
       friends: [{name @(mail: "mariano.achaval@gmail.com", test:123 ): "", id:"test"}, {name: "Mariano", id:null}]
      }  filterObjectLeafs ((value, path) -> !(value is Null or value is String)),
   b: null filterObjectLeafs ((value, path) -> !(value is Null or value is String))
 }
Output
1
2
3
4
5
6
7
8
9
10
{
  a: {
    age: 123,
    friends: [
      {},
      {}
    ]
  },
  b: null
}

filterTree

filterTree(Any, (value: Any, path: Path) → Boolean): Any

Go through the nodes of the value and allows to filter them. The criteria is going to be called with each value and its path. If it returns true the node will remain if false it will be filtered.

Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later. ===== Parameters

Name Description

value

The value to be filtered

criteria

The expression that will determine if the node is filtered or not.

Example

This example shows how the filterTree behaves under different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%dw 2.0
import * from dw::util::Tree
output application/json
---
  {
    a: {name : "", lastName @(foo: ""): "Achaval", friends @(id: 123): [{id: "", test: true}, {age: 123}, ""]} filterTree ((value, path) ->
      value match  {
        case s is String -> !isEmpty(s)
        else -> true
      }
    ),
    b: null filterTree ((value, path) -> value is String),
    c: [{name: "Mariano", friends: []}, {test: [1,2,3]}, {dw: ""}] filterTree ((value, path) -> value match  {
      case a is Array ->  !isEmpty(a as Array)
      else -> true
    }),

  }
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
   a: {
     lastName: "Achaval",
     friends @(id: 123): [
       {
         test: true
       },
       {
         age: 123
       }
     ]
   },
   b: null,
   c: [
     {
       name: "Mariano"
     },
     {
       test: [
         1,
         2,
         3
       ]
     },
     {
       dw: ""
     }
   ]
 }

isArrayType

isArrayType(Path): Boolean

Returns true if the provided path is an Array Type expression

Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later.

Parameters
Name Description

path

The path to be validated

Example

This example shows how the isAttributeType behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::utils::Tree
output application/json
---
{
  a: isArrayType([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}]),
  b: isArrayType([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: ARRAY_TYPE, selector: 0, namespace: null}])
}
Output
1
2
3
4
{
  a: false,
  b: true
}

isAttributeType

isAttributeType(Path): Boolean

Returns true if the provided path is an Object Type expression

Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later.

Parameters
Name Description

path

The path to be validated

Example

This example shows how the isAttributeType behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::util::Tree
output application/json
---
{
  a: isAttributeType([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}]),
  b: isAttributeType([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: OBJECT_TYPE, selector: "name", namespace: null}])
}
Output
1
2
3
4
{
  a: true,
  b: false
}

isObjectType

isObjectType(Path): Boolean

Returns true if the provided path is an Object Type expression

Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later.

Parameters
Name Description

path

The path to be validated

Example

This example shows how the isAttributeType behaves under different inputs.

Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::utils::Tree
output application/json
---
{
  a: isObjectType([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: ATTRIBUTE_TYPE, selector: "name", namespace: null}]),
  b: isObjectType([{kind: OBJECT_TYPE, selector: "user", namespace: null}, {kind: OBJECT_TYPE, selector: "name", namespace: null}])
}
Output
1
2
3
4
{
  a: false,
  b: true
}

mapLeafValues

mapLeafValues(Any, (value: Any, path: Path) → Any): Any

Maps the terminal (leaf) nodes in the tree.

Leafs nodes cannot have an object or an array as a value.

Parameters
Name Description

value

The value to map.

callback

The mapper function.

Example

This example transforms all the string values to upper case.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::util::Tree
output application/json
---
 {
     user: [{
         name: "mariano",
         lastName: "achaval"
     }],
     group: "data-weave"
 } mapLeafValues (value, path) -> upper(value)
Output
1
2
3
4
5
6
7
8
9
{
   "user": [
     {
       "name": "MARIANO",
       "lastName": "ACHAVAL"
     }
   ],
   "group": "DATA-WEAVE"
 }

nodeExists

nodeExists(Any, (value: Any, path: Path) → Boolean): Boolean

Returns true if any node in the tree validates against the specified criteria.

Parameters
Name Description

value

The value to search.

callback

The criteria.

Example

This example checks for any user with the name peter.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
%dw 2.0
import * from dw::util::Tree
output application/json
---
 {
     user: [{
         name: "mariano",
         lastName: "achaval",
         friends: [
             {
                 name: "julian"
             },
             {
                 name: "tom"
             }
         ]
     },
     {
         name: "leandro",
         lastName: "shokida",
         friends: [
             {
                 name: "peter"
             },
             {
                 name: "robert"
             }
         ]

     }
     ],

 } nodeExists ((value, path) -> path[-1].selector == "name" and value == "peter")
Output
1
true

Variables

ARRAY_TYPE

ATTRIBUTE_TYPE

OBJECT_TYPE

Types

Path

Represents an array of PathElement types that identify the location of a node in a tree.

Definition
1
Array<PathElement>

PathElement

Represents a specific selection of a node in a path.

Definition
1
{| kind: "Object" | "Attribute" | "Array", selector: String | Number, namespace: Namespace | Null |}