This module provides functions that allows the user to handle values as if they were Trees

Since Version: 2.2.2

Functions

asExpressionString

asExpressionString(Path): String

Transforms a Path to a String representation.

Parameters
Name Description

path

The path to be transformed to 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 all nodes whose values are leafs. Node of type leafs are all elements where the value is not an Object or an Array

Parameters
Name Description

value

The value to map

callback

The mapper function

Example

This example transforms all the String values to upper

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 of the tree validates against the specified criteria

Parameters
Name Description

value

The value to search

callback

The criteria

Example

This example checks if there is any user with 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, that identifies the location of a Node in a Tree

Definition
1
Array<PathElement>

PathElement

Represents a specific selection node in a Path

Definition
1
2
{| // {options: ["Object","Attribute","Array"]}
kind: String, selector: String | Number, namespace: Namespace | Null |}