---
schema_version: "1"
id: d61f6a5041310055bafd479eca97fead9cd23104aa74b91029c39d43626a420a
title: sizeOf
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.12/dw-core-functions-sizeof.md
canonical_url: https://docs.mulesoft.com/dataweave/2.12/dw-core-functions-sizeof
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0e28db5727eb5bc91e057c8a62c017beac51987ac432ddaf7b443fd0a1416ea4
description: MuleSoft Documentation Site
release: "2.12"
latest: false
---
---
product: DataWeave
version: 2.12
---

# sizeOf

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## sizeOf(array: 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>array</code></p></td><td><p>The input array. The elements in the array can be any supported type.</p></td></tr></tbody></table>

### Example

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

#### Source

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

#### Output

```JSON
3
```

### Example

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

#### Source

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

#### Output

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

## sizeOf(object: 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>object</code></p></td><td><p>The input object that contains one or more key-value pairs.</p></td></tr></tbody></table>

### Example

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

#### Source

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

#### Output

```JSON
2
```

### Example

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

#### Source

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

#### Output

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

## sizeOf(binary: Binary): Number

Returns the size of a binary payload in bytes. Returns 0 if the payload is empty.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>binary</code></p></td><td><p>The input array of binary values.</p></td></tr></tbody></table>

### Example

This example returns the number of bytes in the binary payload. Notice how multi-byte UTF-8
characters take up more than one byte.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{

BinarySizeOf: sizeOf("'my word'" as Binary) ++ " " ++ sizeOf("'my word'" as Binary),

StringSizeOf: sizeOf("'my word'") ++ " " ++ sizeOf("'my word'")

}
```

#### Output

```JSON
{

"BinarySizeOf": "9 9",

"StringSizeOf": "9 9"

}
```

## sizeOf(text: String): Number

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

Returns `0` if the string is empty.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The input text.</p></td></tr></tbody></table>

### Example

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

#### Source

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

#### Output

```JSON
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’s not a string.

#### Source

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

#### Output

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

## sizeOf(value: Period): Number

Returns the number of characters in a `Period` value.

_Introduced in DataWeave version 2.6.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The input <code>Period</code>.</p></td></tr></tbody></table>

### Example

This example returns the number of characters in the `Period` value.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{
  a: sizeOf(|P3D|)
}
```

#### Output

```JSON
{ "a": 3 }
```

## sizeOf(value: DateTime): Number

Returns the number of characters in a `DateTime` value.

_Introduced in DataWeave version 2.6.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The input <code>DateTime</code>.</p></td></tr></tbody></table>

### Example

This example returns the number of characters in the `DateTime` value.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{
  a: sizeOf(|2025-07-13T18:06:59.314033Z|)
}
```

#### Output

```JSON
{ "a": 27 }
```

## sizeOf(value: LocalDateTime): Number

Returns the number of characters in a `LocalDateTime` value.

_Introduced in DataWeave version 2.6.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The input <code>LocalDateTime</code>.</p></td></tr></tbody></table>

### Example

This example returns the number of characters in the `LocalDateTime` value.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{
  a: sizeOf(|2025-07-13T18:06:59.314033|)
}
```

#### Output

```JSON
{ "a": 26 }
```

## sizeOf(number: Number): Number

Returns the number of characters in a `Number` value.

To keep backward compatibility with 2.4, this function returns `1` for any `Number` value when the
flag `com.mulesoft.dw.legacySizeOfNumber` is enabled.

_Introduced in DataWeave version 2.6.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>number</code></p></td><td><p>The input number.</p></td></tr></tbody></table>

### Example

This example returns the number of characters in the `Number` value.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{
  a: sizeOf(123),
  b: sizeOf(123.45)
}
```

#### Output

```JSON
{ "a": 3, "b": 6 }
```

### Example

This example shows how the `sizeOf` function works when the flag
`com.mulesoft.dw.legacySizeOfNumber` is enabled. Notice it returns `1` for any given input.

#### Source

```DataWeave
%dw 2.0
output application/json
---
{
  a: sizeOf(123)
  b: sizeOf(123.45)
}
```

#### Output

```JSON
{ "a": 1, "b": 1 }
```

## sizeOf(n: Null): Null

Helper function that enables `sizeOf` to work with a `null` value.

_Introduced in DataWeave version 2.4.0._
