# Convert

> Convert a value to another datatype, optionally picking a field or formatting a string; failures go to error.

Source: https://docs.tune.toys/nodes/data.convert/

Convert changes a value's datatype. It can first pick one field out of JSON with a pointer, and it can write values into a text template.

## What it does

A value arrives on `value`. If the node's `path` field holds a JSON pointer such as `/current/temperature_2m` or `/0/name`, Convert picks that field first. It then converts to the To datatype and sends the result on `value`. The text `"21.5"` becomes the number 21.5, `1` becomes `true`, a list of objects becomes a table, and so on.

With To String and a `format` template, `{value}` is replaced by the value and `{/pointer}` by a field, so `It is {value} °C` reads `It is 21.5 °C`.

A value that cannot be converted, or a pointer that finds nothing, is never thrown away silently: the reason goes to `error` and nothing is sent on `value`.

## When to use it

Data cables only connect when their datatypes fit; widening such as integer to number happens on its own, but narrowing does not. Use Convert between a [Web request](https://docs.tune.toys/nodes/web.request/) response and a number input, before [Data to control](https://docs.tune.toys/nodes/data.to-control/), or to turn a [Value](https://docs.tune.toys/nodes/data.value/) into a URL or prompt string.

## Ports

<!-- ports:start -->

| Port    | Direction | Signal        | Lanes |
| ------- | --------- | ------------- | ----- |
| `value` | Input     | Data (any)    | 1     |
| `value` | Output    | Data (any)    | 1     |
| `error` | Output    | Data (string) | 1     |

<!-- ports:end -->

## Parameters

<!-- parameters:start -->

| Parameter | Label | Range  | Default | Unit | Scale  | Notes                                                                                                          |
| --------- | ----- | ------ | ------- | ---- | ------ | -------------------------------------------------------------------------------------------------------------- |
| `to`      | To    | 0 to 6 | 0       |      | linear | Datatype of the converted value. Choices: 0 Number, 1 Integer, 2 Boolean, 3 String, 4 Colour, 5 JSON, 6 Table. |

<!-- parameters:end -->

<!-- data:start -->

- `path`: pointer.
- `format`: text.

<!-- data:end -->

### To (`to`)

Number, Integer, Boolean, String, Colour, JSON or Table. The `value` output takes this datatype.

### Path and format

The `path` field is a JSON pointer (up to 256 characters); leave it empty to convert the whole value. The `format` field is the String template (up to 1 024 characters).

## Tips

- Cable `error` into a [Console log](https://docs.tune.toys/nodes/log.console/) at Error level while you find the right pointer.
- List indexes in pointers start at 0: `/results/0/name/first`.

## See also

[Data to control](https://docs.tune.toys/nodes/data.to-control/), [Dataset](https://docs.tune.toys/nodes/data.set/), [Web request](https://docs.tune.toys/nodes/web.request/), [Data cables](https://docs.tune.toys/concepts/data-cables/).
