nodeExists

nodeExists(value: Any, callback: (value: Any, path: Path) -> Boolean): Boolean

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

Introduced in DataWeave version 2.2.2.

Parameters

Name Description

value

The value to search.

callback

The criteria to apply to the input value.

Example

This example checks for each user by name and last name. Notice that you can also reference a value with $ and the path with $$.

Source

%dw 2.0
import * from dw::util::Tree
var myObject =  {
     user: [{
         name: "mariano",
         lastName: "achaval",
         friends: [
             {
                 name: "julian"
             },
             {
                 name: "tom"
             }
         ]
     },
     {
         name: "leandro",
         lastName: "shokida",
         friends: [
             {
                 name: "peter"
             },
             {
                 name: "robert"
             }
         ]

     }
     ]
 }
output application/json
---
{
    mariano : myObject nodeExists ((value, path) -> path[-1].selector == "name" and value == "mariano"),
    julian : myObject nodeExists ((value, path) -> path[-1].selector == "name" and value == "julian"),
    tom : myObject nodeExists ($$[-1].selector == "name" and $ == "tom"),
    leandro : myObject nodeExists ($$[-1].selector == "name" and $ ==  "leandro"),
    peter : myObject nodeExists ($$[-1].selector == "name" and $ == "peter"),
    wrongField: myObject nodeExists ($$[-1].selector == "wrongField"),
    teo: myObject nodeExists ($$[-1].selector == "name" and $ == "teo")
}

Output

{
  "mariano": true,
  "julian": true,
  "tom": true,
  "leandro": true,
  "peter": true,
  "wrongField": false,
  "teo": false
}

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub