# Transform Data

> Transforms the input payload using powerful JSONata expressions.

Source: https://docs.sommatic.ai/en/nodes/data.transform/

- Slug: `data.transform`
- Family: Data
- Version: 1.0.0
- Status: Available
- Execution: sync

Transforms the input payload using powerful JSONata expressions.

This is the primary tool for data mapping and structural transformation logic within the workflow. It allows you to reshape, filter, and mathematically compute new values from the input JSON.

**Features:**
- Full JSONata syntax support.
- Access to the entire workflow context.
- Capability to query and aggregate data.

**Use Cases:**
- Mapping an API response to an internal model.
- Filtering items from a list (e.g., `items[price > 50]`).
- Calculating totals (e.g., `$sum(items.price)`).

## Configuration

- `expr` (string, required, accepts expressions): The JSONata expression to evaluate. The result becomes the output payload.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Transformation result
- `error`: Expression failed

## Input contract

- `payload` (object, optional)

## Output contract

- `result` (object, optional)

## In expressions

What a later node can read from this node's output with `$("Transform Data").item.json.<path>` (write your node's label instead of `Transform Data`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Transform Data").item.json.result` (object) **The worker does not return it: reads as undefined.**
- `$("Transform Data").item.json.id` (string) = `"ord_123"` (Not suggested by the editor)
- `$("Transform Data").item.json.total` (number) = `42.5` (Not suggested by the editor)
- `$("Transform Data").item.json.lines` (number) = `2` (Not suggested by the editor)


## How it works

Evaluates a JSONata expression against the incoming payload; the result replaces the payload.

1. Resolves the config: `{{ }}` blocks inside `expr` are evaluated first by the Expression Engine; plain JSONata text is left untouched.
2. Fails when `expr` is empty.
3. If `expr` resolved to an object or array (a whole-value `{{ }}` returning data), that value is the result and JSONata is skipped.
4. Otherwise compiles the string as JSONata 2 and evaluates it with the payload as root: `order.total`, `$sum(items.price)`, `{ "id": order_id, "lines": $count(items) }`.
5. Returns the result as the new payload through `out`.

## In a workflow

### Node

```json
{
  "node_id": "node-shape-order",
  "label": "Shape order",
  "operator_slug": "data.transform",
  "operator_version": "1.0.0",
  "config": {
    "expr": "{ \"id\": order_id, \"total\": $sum(items.price), \"lines\": $count(items) }"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "id": "ord_123",
    "total": 42.5,
    "lines": 2
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The expression was evaluated.

```json
{
  "id": "ord_123",
  "total": 42.5,
  "lines": 2
}
```

### `error`

`expr` is empty or JSONata failed.

```json
{
  "ok": false,
  "errors": [
    "Transform Error: Unexpected end of expression"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `expr`: Expression Engine variables are not bound inside JSONata: `$json.total` evaluates to undefined. Use payload paths and JSONata built-ins (`$now()`, `$sum`, `$map`), or embed engine values with `{{ }}` in mixed text, which inserts their string form: `{ "run": "{{ $execution.id }}", "total": $sum(items.price) }`. A failing `{{ }}` in mixed text becomes `[ERROR: <message>]` inside the expression.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Transformation expression is required` | `error` | `expr` is missing, or its `{{ }}` resolved to an empty value. |
| `Transform Error: <JSONata message>` | `error` | JSONata syntax or runtime error, for example `Unexpected end of expression`. |
| `Transform Error: path.charAt is not a function` | `error` | A whole-value `{{ }}` returned a number or boolean, which is neither JSONata text nor data. |

## Notes and limits

- If the result is undefined (a path that matches nothing) or falsy, the engine stores the whole envelope `{ ok, port }` as the output instead of the result.
- The previous payload is replaced; reference earlier data with `{{ $("Node").item.json.field }}` in mixed text.
- When the envelope has no `data` (or `data` is falsy), the engine stores the whole envelope as the node output: on `error` the next node receives `{ ok: false, errors: [...], port: "error" }` as its payload.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "data.transform",
  "name": "Transform Data",
  "category": "data",
  "version": "1.0.0",
  "description": "Transforms the input payload using powerful JSONata expressions.\n\n  This is the primary tool for data mapping and structural transformation logic within the workflow. It allows you to reshape, filter, and mathematically compute new values from the input JSON.\n\n  **Features:**\n  - Full JSONata syntax support.\n  - Access to the entire workflow context.\n  - Capability to query and aggregate data.\n\n  **Use Cases:**\n  - Mapping an API response to an internal model.\n  - Filtering items from a list (e.g., `items[price > 50]`).\n  - Calculating totals (e.g., `$sum(items.price)`).",
  "capabilities": [
    "transform",
    "jsonata",
    "mapping",
    "utils"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "expr"
    ],
    "properties": {
      "expr": {
        "type": "string",
        "title": "Transformation Expression",
        "format": "expression",
        "description": "The JSONata expression to evaluate. The result becomes the output payload."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "description": "Any JSON payload.",
    "properties": {
      "payload": {
        "type": "object"
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "The result of the JSONata evaluation.",
    "properties": {
      "result": {
        "type": "object"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Output",
      "is_default": true,
      "description": "Transformation result"
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Expression failed"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "transform",
    "jsonata",
    "map"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "expr"
  ],
  "properties": {
    "expr": {
      "type": "string",
      "title": "Transformation Expression",
      "format": "expression",
      "description": "The JSONata expression to evaluate. The result becomes the output payload."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "description": "Any JSON payload.",
  "properties": {
    "payload": {
      "type": "object"
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "The result of the JSONata evaluation.",
  "properties": {
    "result": {
      "type": "object"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Output",
    "is_default": true,
    "description": "Transformation result"
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Expression failed"
  }
]
```
