Creating Data
In this tutorial we will go over how to create:
- Numbers
- Strings
- Booleans
- Arrays
- Objects
These values have their respective types:
- Number
- String
- Boolean
- Array
- Object or {}
As you’d expect, the last two are actually composite types, their contents also have type definitions:
- For an array, it means its type will be Array<T> where T is a type parameter that defines the type of the elements inside the array. So the type of an array of strings would be Array<String>.
- For objects, their key-value pairs could define types of their own. So the type of an object with a name string key would be {name : String}.
You can check the type of a value by using typeOf:
DW Script:
%dw 2.0
output json
—
typeOf({})
Output:
“Object”
Numbers (dw::core::Numbers)
This module contains helper functions to work with Numbers.
To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Numbers to the header of your DataWeave script.
Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.
DataWeave also supports numbers with the Number type, covering both integer and floating-point numbers.
DW Script:
%dw 2.0
output json
—
1
Output:
1
Strings (dw::core::Strings)
This module contains helper functions for working with strings.
Strings are defined between quotes. For example, the following script takes no input, it just outputs the String “hello”:
DW Script:
%dw 2.0
output json
—
“hello”
Output:
“hello”
To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Strings to the header of your DataWeave script.
Functions
Name | Description |
appendIfMissing | Appends the suffix to the end of the text if the text does not already ends with the suffix. |
camelize | Returns a string in camel case based on underscores in the string. |
capitalize | Capitalizes the first letter of each word in a string. |
charCode | Returns the Unicode for the first character in an input string. |
charCodeAt | Returns the Unicode for a character at the specified index. |
collapse | Collapses the string into substrings of equal characters. |
countCharactersBy | Counts the number of times an expression that iterates through each character in a string returns true. |
countMatches | Counts the number of matches in a string. |
dasherize | Replaces spaces, underscores, and camel-casing in a string with dashes (hyphens). |
everyCharacter | Checks whether a condition is valid for every character in a string. |
first | Returns characters from the beginning of a string to the specified number of characters in the string, for example, the first two characters of a string. |
fromCharCode | Returns a character that matches the specified Unicode. |
hammingDistance | Returns the Hamming distance between two strings. |
isAlpha | Checks if the text contains only Unicode digits. A decimal point is not a Unicode digit and returns false. |
isAlphanumeric | Checks if the text contains only Unicode letters or digits. |
isLowerCase | Checks if the text contains only lowercase characters. |
isNumeric | Checks if the text contains only Unicode digits. |
isUpperCase | Checks if the text contains only uppercase characters. |
isWhitespace | Checks if the text contains only whitespace. |
last | Returns characters from the end of string to a specified number of characters, for example, the last two characters of a string. |
leftPad | The specified text is left-padded to the size using the padText. By default padText is ” “. |
levenshteinDistance | Returns the Levenshtein distance (or edit distance) between two strings. |
lines | Returns an array of lines from a string. |
mapString | Applies an expression to every character of a string. |
ordinalize | Returns a number as an ordinal, such as 1st or 2nd. |
pluralize | Pluralizes a singular string. |
prependIfMissing | Prepends the prefix to the beginning of the string if the text does not already start with that prefix. |
remove | Removes all occurrences of a specified pattern from a string. |
repeat | Repeats a text the number of specified times. |
replaceAll | Replaces all substrings that match a literal search string with a specified replacement string. |
reverse | Reverses sequence of characters in a string. |
rightPad | The specified text is right-padded to the size using the padText. By default padText is ” “. |
singularize | Converts a plural string to its singular form. |
someCharacter | Checks whether a condition is valid for at least one of the characters or blank spaces in a string. |
substring | Returns a substring that spans from the character at the specified from index to the last character before the until index. |
substringAfter | Gets the substring after the first occurrence of a separator. The separator is not returned. |
substringAfterLast | Gets the substring after the last occurrence of a separator. The separator is not returned. |
substringBefore | Gets the substring before the first occurrence of a separator. The separator is not returned. |
substringBeforeLast | Gets the substring before the last occurrence of a separator. The separator is not returned. |
substringBy | Splits a string at each character where the predicate expression returns true. |
substringEvery | Splits a string into an array of substrings equal to a specified length. |
underscore | Replaces hyphens, spaces, and camel-casing in a string with underscores. |
unwrap | Unwraps a given text from a wrapper text. |
withMaxSize | Checks that the string length is no larger than the specified maxLength. If the string’s length is larger than the maxLength, the function cuts characters from left to right, until the string length meets the length limit. |
words | Returns an array of words from a string. |
wrapIfMissing | Wraps text with wrapper if that wrapper is missing from the start or end of the given string. |
wrapWith | Wraps the specified text with the given wrapper. |
Boolean (dw::Core Type)
A Boolean is defined by the keywords true and false.
This is the last simple type we’ll cover in this tutorial. The Boolean type only has two values: true and false.
DW Script:
%dw 2.0
output json
—
{
“yes”: true,
“no”: false
}
Output:
{
“yes”: true,
“no”: false
}
Booleans are valuable when it comes to conditional logic (i.e., “if something is true, do this, if it’s false, do that”) which we will cover in another tutorial.
Arrays (dw::core::Arrays)
This module contains helper functions for working with arrays.
Arrays are an ordered series of values where the values can be of any type:
DW Script:
%dw 2.0
output json
—
[“1”, 2, “3”, 4, “five”]
Output:
[“1”, 2, “3”, 4, “five”]
To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::core::Arrays to the header of your DataWeave script.
Functions
Name | Description |
countBy | Counts the elements in an array that return true when the matching function is applied to the value of each element. |
divideBy | Breaks up an array into sub-arrays that contain the specified number of elements. |
drop | Drops the first n elements. It returns the original array when n <= 0 and an empty array when n > sizeOf(array). |
dropWhile | Drops elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition. |
every | Returns true if every element in the array matches the condition. |
firstWith | Returns the first element that satisfies the condition, or returns null if no element meets the condition. |
indexOf | Returns the index of the first occurrence of an element within the array. If the value is not found, the function returns -1. |
indexWhere | Returns the index of the first occurrence of an element that matches a condition within the array. If no element matches the condition, the function returns -1. |
join | Joins two arrays of objects by a given ID criteria. |
leftJoin | Joins two arrays of objects by a given ID criteria. |
outerJoin | Joins two array of objects by a given ID criteria. |
partition | Separates the array into the elements that satisfy the condition from those that do not. |
slice | Selects the interval of elements that satisfy the condition: from <= indexOf(array) < until |
some | Returns true if at least one element in the array matches the specified condition. |
splitAt | Splits an array into two at a given position. |
splitWhere | Splits an array into two at the first position where the condition is met. |
sumBy | Returns the sum of the values of the elements in an array. |
take | Selects the first n elements. It returns an empty array when n <= 0 and the original array when n > sizeOf(array). |
takeWhile | Selects elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition. |
Object (dw::Core Type) or {}
Represents any object as a collection of Key:value pairs, where a Key is composed of a Name and Attributes.
The Name type is composed of a String as the local name and the Namespace. Attributes are composed of an Array of Name:value pairs. Note that the Key is not a String, so it is not possible to compare keys. However, you can get the local name by performing an as String type coercion on any value of type Key.
Example
%dw 2.0
output application/json
—
{
name: “Annie”
}
Objects are a series of key-value mappings, where the value can be of any type:
DW Script:
%dw 2.0
output json
—
{
one: [1],
two: “2”,
three: {
A: “AAA”
}
}
Output:
{
“one”: [1],
“two”: “2”,
“three”: {
“A”: “AAA”
}
}
Single Value Objects
If an object has only one key:value pair, the enclosing curly braces { } are not required:
Example
%dw 2.0
output application/json
—
name: “Annie”
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
Leave a Comment