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"

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
12
13
14
%dw 2.0
output application/json
---
%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
34
35
36
%dw 2.0
output application/json
---
* %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
2
{| // {options: ["Object","Attribute","Array"]}
kind: String, selector: String | Number, namespace: Namespace | Null |}