Contact Us 1-800-596-4880

sizeOf

DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended.

Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted.

MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements.

sizeOf(Array<Any>): Number

Returns the number of elements in an array. It returns 0 if the array is empty.

This version of sizeOf takes an array or an array of arrays as input. Other versions act on arrays of objects, strings, or binary values.

Parameters

Name Description

array

The input array. The elements in the array can be any supported type.

Example

This example counts the number of elements in the input array. It returns 3.

Source

%dw 2.0
output application/json
---
sizeOf([ "a", "b", "c"])

Output

3

Example

This example returns a count of elements in the input array.

Source

%dw 2.0
output application/json
---
{
  "arraySizes": {
     size3: sizeOf([1,2,3]),
     size2: sizeOf([[1,2,3],[4]]),
     size0: sizeOf([])
   }
}

Output

{
   "arraySizes": {
     "size3": 3,
     "size2": 2,
     "size0": 0
   }
}

sizeOf(Object): Number

Returns the number of key-value pairs in an object.

This function accepts an array of objects. Returns 0 if the input object is empty.

Parameters

Name Description

object

The input object that contains one or more key-value pairs.

Example

This example counts the key-value pairs in the input object, so it returns 2.

Source

%dw 2.0
output application/json
---
sizeOf({a: 1, b: 2})

Output

2

Example

This example counts the key-value pairs in an object.

Source

%dw 2.0
output application/json
---
{
   objectSizes : {
     sizeIs2: sizeOf({a:1,b:2}),
     sizeIs0: sizeOf({})
   }
}

Output

{
  "objectSize": {
    "sizeIs2": 2,
    "sizeIs0": 0
  }
}

sizeOf(Binary): Number

Returns the number of elements in an array of binary values.

Parameters

Name Description

binary

The input array of binary values.

Example

This example returns the size of an array of binary values.

Source

%dw 2.0
output application/json
---
sizeOf(["\u0000" as Binary, "\u0001" as Binary, "\u0002" as Binary])

Output

3

sizeOf(String): Number

Returns the number of characters (including white space) in an string.

Returns 0 if the string is empty.

Parameters

Name Description

text

The input text.

Example

This example returns the number of characters in the input string "abc".

Source

%dw 2.0
output application/json
---
sizeOf("abc")

Output

3

Example

This example returns the number of characters in the input strings. Notice it counts blank spaces in the string "my string" and that sizeOf("123" as Number) returns 1 because 123 is coerced into a number, so it is not a string.

Source

%dw 2.0
output application/json
---
{
  sizeOfSting2 : sizeOf("my string"),
  sizeOfEmptyString: sizeOf(""),
  sizeOfNumber : sizeOf("123" as Number)
}

Output

{
  "sizeOfSting2": 9,
  "sizeOfEmptyString": 0,
  "sizeOfNumber": 1
}