Creating Data

 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

NameDescription
appendIfMissingAppends the suffix to the end of the text if the text does not already ends with the suffix.
camelizeReturns a string in camel case based on underscores in the string.
capitalizeCapitalizes the first letter of each word in a string.
charCodeReturns the Unicode for the first character in an input string.
charCodeAtReturns the Unicode for a character at the specified index.
collapseCollapses the string into substrings of equal characters.
countCharactersByCounts the number of times an expression that iterates through each character in a string returns true.
countMatchesCounts the number of matches in a string.
dasherizeReplaces spaces, underscores, and camel-casing in a string with dashes (hyphens).
everyCharacterChecks whether a condition is valid for every character in a string.
firstReturns 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.
fromCharCodeReturns a character that matches the specified Unicode.
hammingDistanceReturns the Hamming distance between two strings.
isAlphaChecks if the text contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
isAlphanumericChecks if the text contains only Unicode letters or digits.
isLowerCaseChecks if the text contains only lowercase characters.
isNumericChecks if the text contains only Unicode digits.
isUpperCaseChecks if the text contains only uppercase characters.
isWhitespaceChecks if the text contains only whitespace.
lastReturns characters from the end of string to a specified number of characters, for example, the last two characters of a string.
leftPadThe specified text is left-padded to the size using the padText. By default padText is ” “.
levenshteinDistanceReturns the Levenshtein distance (or edit distance) between two strings.
linesReturns an array of lines from a string.
mapStringApplies an expression to every character of a string.
ordinalizeReturns a number as an ordinal, such as 1st or 2nd.
pluralizePluralizes a singular string.
prependIfMissingPrepends the prefix to the beginning of the string if the text does not already start with that prefix.
removeRemoves all occurrences of a specified pattern from a string.
repeatRepeats a text the number of specified times.
replaceAllReplaces all substrings that match a literal search string with a specified replacement string.
reverseReverses sequence of characters in a string.
rightPadThe specified text is right-padded to the size using the padText. By default padText is ” “.
singularizeConverts a plural string to its singular form.
someCharacterChecks whether a condition is valid for at least one of the characters or blank spaces in a string.
substringReturns a substring that spans from the character at the specified from index to the last character before the until index.
substringAfterGets the substring after the first occurrence of a separator. The separator is not returned.
substringAfterLastGets the substring after the last occurrence of a separator. The separator is not returned.
substringBeforeGets the substring before the first occurrence of a separator. The separator is not returned.
substringBeforeLastGets the substring before the last occurrence of a separator. The separator is not returned.
substringBySplits a string at each character where the predicate expression returns true.
substringEverySplits a string into an array of substrings equal to a specified length.
underscoreReplaces hyphens, spaces, and camel-casing in a string with underscores.
unwrapUnwraps a given text from a wrapper text.
withMaxSizeChecks 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.
wordsReturns an array of words from a string.
wrapIfMissingWraps text with wrapper if that wrapper is missing from the start or end of the given string.
wrapWithWraps 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

NameDescription
countByCounts the elements in an array that return true when the matching function is applied to the value of each element.
divideByBreaks up an array into sub-arrays that contain the specified number of elements.
dropDrops the first n elements. It returns the original array when n <= 0 and an empty array when n > sizeOf(array).
dropWhileDrops 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.
everyReturns true if every element in the array matches the condition.
firstWithReturns the first element that satisfies the condition, or returns null if no element meets the condition.
indexOfReturns the index of the first occurrence of an element within the array. If the value is not found, the function returns -1.
indexWhereReturns 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.
joinJoins two arrays of objects by a given ID criteria.
leftJoinJoins two arrays of objects by a given ID criteria.
outerJoinJoins two array of objects by a given ID criteria.
partitionSeparates the array into the elements that satisfy the condition from those that do not.
sliceSelects the interval of elements that satisfy the condition: from <= indexOf(array) < until
someReturns true if at least one element in the array matches the specified condition.
splitAtSplits an array into two at a given position.
splitWhereSplits an array into two at the first position where the condition is met.
sumByReturns the sum of the values of the elements in an array.
takeSelects the first n elements. It returns an empty array when n <= 0 and the original array when n > sizeOf(array).
takeWhileSelects 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.

Instagram

Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *