Convert

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

  • Data
  • Since 0.2.0
  • convert
  • cast
  • json
  • pointer
  • format
  • template
  • number
  • data

Open in TuneToys

Convert as the Patch draws it, at default values: inlets on the left, outlets on the right.

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 response and a number input, before Data to control, or to turn a Value into a URL or prompt string.

Ports#

PortDirectionSignalLanes
valueInputData (any)1
valueOutputData (any)1
errorOutputData (string)1

Parameters#

ParameterLabelRangeDefaultUnitScaleNotes
toTo0 to 60linearDatatype of the converted value. Choices: 0 Number, 1 Integer, 2 Boolean, 3 String, 4 Colour, 5 JSON, 6 Table.
  • path: pointer.
  • format: text.

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 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, Dataset, Web request, Data cables.

Help patches

Each help patch is an ordinary Toy that teaches one idea. The diagram shows the Patch; the preview shows its Frame. Open it in TuneToys to play and edit it.

1. Pick a field

beginner Teaches: to

Convert picks one field out of a JSON object with a pointer and turns it into a number.

Patch: Value value output connected to Convert “celsius” value input. Convert “celsius” value output connected to Console log “log” value input. Convert “celsius” value output connected to Convert “label” value input. Convert “label” value output connected to Console log “say” value input.
Connections as text
  • Value value output connected to Convert “celsius” value input
  • Convert “celsius” value output connected to Console log “log” value input
  • Convert “celsius” value output connected to Convert “label” value input
  • Convert “label” value output connected to Console log “say” value input
  • path /sensor/celsius picks the field; To Number parses the text "21.5".
  • To String with a format template writes {value} into a sentence.
Frame preview: Pick a fieldFrame “Pick a field” with 2 elements: text heading, text hint.PICK A FIELDOpen the Console: 21.5 as a number, then the sentence the template made from it.
Frame preview (static). Controls show their default values.

Open in TuneToys

  • Data to control Turn numbers into one control lane for a parameter or control input, mapped from a declared range.
  • Web request Fetch a URL on each trigger under the network allowlist; secrets come from a vault slot, never the Toy.
  • Dataset Read CSV, TSV, JSON or NDJSON from an asset, a host-chosen file or a connection as rows, JSON and text.

Edit this page on GitHub

esc

↑ ↓ move↩ open⌘↩ new tabesc close