Search

Dark theme | Light theme
Showing posts with label DataWeave:Syntax. Show all posts
Showing posts with label DataWeave:Syntax. Show all posts

September 27, 2022

DataWeave Delight: Add Conditionally Element to Array or Object

The core of DataWeave is to transform data from one format to another format. If we want to include an element in an array based on a condition we can enclose the element between parenthesis (element) followed by an if statement. If the condition of the if statement returns true the element is included in the array. To include a key/value pair in an object based on a condition we also enclose the key/value pair in parenthesis (key: value) and add an if statement. When the condition specified is true the key/value pair is added to the object, otherwise it will be left out.

In the following example we add conditionally the key website and each element of the array is added based on a condition. The conditions check values in a conf object defined at the top of the example:

Source

%dw 2.0

// Helper object with values used to add elements conditionally.
var conf = {
    kind: "FP",
    includeWebsite: true
}

output application/json
---
[
    ({
        language: "DataWeave", 
        kind: "Functional",
        // Add key/value to object if condition is true.
        (website: "https://docs.mulesoft.com/dataweave/2.4/") if conf.includeWebsite
    }) 
    // Add element to array if condition is true.
    if conf.kind == "FP", 
    
    ({
        language: "Java", 
        kind: "Object Oriented",
        (website: "https://www.java.com/") if (conf.includeWebsite)
    }) if (conf.kind == "OO")
]

Result

[
    {
        "language": "DataWeave",
        "kind": "Functional",
        "website": "https://docs.mulesoft.com/dataweave/2.4/"
    }
]

When we change the values in our conf object we get a different result:

Source

var conf = {
    kind: "OO",
    includeWebsite: false
}

Result

[
    {
        "language": "Java",
        "kind": "Object Oriented"
    }
]

Written with DataWeave 2.4.

February 9, 2022

DataWeave Delight: Using dynamic elements to build a new object

When we create an object structure it is very useful to use dynamic elements. Dynamic elements can turn an object or array of objects into key and value pairs in an object constructor. The syntax of the dynamic element is that an object or array of objects is enclosed in parentheses ((...)). We use this inside an object constructor enclosed in curly braces ({...}) so we get the key/value pairs. The objects can be referenced by a variable or a function that returns an object.

In the following example we use dynamic elements to create a object with key/value pairs from an object and array of objects:

Source

%dw 2.0

var messageObj = {
    message0: {
        text: "Hello, DataWeave"
    }
}

fun message(index: Number, prefix:String , name: String): Object = {
    ("message" ++ index): {
        text: "$(prefix), $(name)"
    }
}

output application/json
---
{
    // Dynamic element for a single object
    ({message: text: "Hello from object"}),

    // Dynamic element for a single object referenced by variable
    (messageObj),

    // Dynamic element for function that returns an object
    (message(1, "Goodbye", "Java")),

    // Dynamic element on array of expressions that return objects
    ([message(2, "Hi", "mrhaki"), message(3, "Good evening", "Hubert")]), 

    // Simple key/value that will be part of the final object
    created_with: "DataWeave 2.4"
}

Output

{
  "message": {
    "text": "Hello from object"
  },
  "message0": {
    "text": "Hello, DataWeave"
  },
  "message1": {
    "text": "Goodbye, Java"
  },
  "message2": {
    "text": "Hi, mrhaki"
  },
  "message3": {
    "text": "Good evening, Hubert"
  },
  "created_with": "DataWeave 2.4"
}

Written with DataWeave 2.4.

February 6, 2022

DataWeave Delight: Using string interpolation

In DataWeave we can use expressions in strings that will be evaluated and inserted into the string value. This is called string interpolation. The expression must be enclosed in parentheses where the first parenthesis is prefixed with a dollar sign: $(<expression>). The expression must return a string value or can be automatically coerced into a string value in order for it to work. The expression can also be a variable.

In the following example we use a variable, selector, the upper function and a calculation that returns a number that is coerced into a string:

Source

%dw 2.0

var prefix = "Hi"

var user = { alias: "mrhaki", name: "Hubert Klein Ikkink"}

var count = 41

output text/plain
---
"$(prefix), 
welcome $(user.name) also known as $(upper(user.alias)).
You're visitor $(count + 1)."

Output

Hi, 
welcome Hubert Klein Ikkink also known as MRHAKI.
You're visitor 42.

Written with DataWeave 2.4.