---
url: https://docs.juniper.bot/en/message-templates/types/type-convert.md
description: Data Type Conversion
---

# Type Conversion

Template Engine will try to automatically convert the input data value to the expected data type.

## Conversion Rules { #rules }

For some data types, values can be automatically converted following the rules:

### Numbers { #numbers }

If [Number](index#literals) is expected, values of other data types can be converted as follows:

| Source                     | Number Representative                      |
|:---------------------------|:-------------------------------------------|
| Null Reference (`null`)    | 0                                          |
| Undefined                  | 0                                          |
| Boolean                    | 1 for `true`, 0 for `false`                |
| String                     | Will try to parse the number in the string |
| [DateTime](index#datetime) | Will convert to timestamp                  |

### Boolean { #boolean }

If [Boolean](index#literals) is expected, values of other data types can be converted as follows:

| Source                  | Boolean Representative              |
|:------------------------|:------------------------------------|
| Null Reference (`null`) | `false`                             |
| Undefined               | `false`                             |
| String                  | `false`  if empty, `true` otherwise |
| List or Map             | `false`  if empty, `true` otherwise |
| Number                  | `false` for 0, `true` otherwise     |

### String { #string }

If [String](index#literals) is expected, values of other data types will be converted to string to the same form as it
would print it using [output](../advanced/code-islands#output) code island.

### DateTime { #datetime }

If [DateTime](index#datetime) is expected, values of other data types can be converted as follows:

| Source | DateTime Representative                                                                                                                                                                            |
|:-------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| String | Will try to parse the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) string: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`If string is `now`, will be converted to current datetime. |
| Number | Will convert to date using this number as Unix timestamp                                                                                                                                           |

## Where it works? { #scope }

Automatic data type conversion is available for:

* [Operators and Expressions](../advanced/expressions);
* [`if` constructs](../advanced/tags#if);
* [Functions](../advanced/functions) arguments;
* [Methods](index) parameters with some exceptions and special behavior.

::: tip Data Type Conversion in Methods Parameters

* Doesn't work for some [list](index#lists-methods) and [map](index#maps-methods) methods.
* If a method parameter is documented with multiple data types (so-called overloads), the template engine will attempt
  to convert them in the order specified there.
  :::

## Comparison Operations { #comparison }

[Binary Comparison Operators](../advanced/expressions#binary) (`>`, `>=`, `<`, `<=`, `in`, `==`, `!=`) use the following algorithm:

1. If both operands can be converted to a [numbers](#numbers), they are converted and compared as numbers;
2. If at least one of the operands cannot be converted to a number, both operands are converted to strings and compared as strings.

This algorithm is universal, but in some cases the order of operations should be taken into account. For example:

```template
{{ 'true' == 1 == true }}
{{ true == 1 == 'true' }}
```

It would seem that both expressions should print `true`, but the first one prints `false`.

First expression:

1. `'true' == 1` evaluates as `false`, because **string** `'true'` cannot be converted to a [number](#numbers), so both
   operands are converted to strings and compared as `'true'` and `'1'` that gives `false` in result;
2. Second operation becomes `false == true` that gives `false` as the final result of the whole expression.

Second expression:

1. `true == 1` evaluates as `true`, because **boolean** `true` can be converted to number `1`, so both operands are compared as numbers (they are equal);
2. Second operation becomes `true == 'true'` that gives `true` as the final result of the whole expression, but only
   because second operand `'true'` is **string** that cannot be converted to a number, so both operands are converted to
   strings and compared as `'true'` and `'true'` (they are equal).
