本页内容
Sommatic 文档
打开控制台

Transform Data

使用强大的 JSONata 表达式转换输入载荷。

以 Markdown 查看
在以下应用中打开
数据 可用 data.transform 版本 1.0.0 执行方式: sync

针对输入 payload 求值 JSONata 表达式;结果替换 payload。

这是工作流中进行数据映射和结构转换的主要工具。它可以对输入 JSON 进行重塑、过滤,并通过数学计算生成新值。

功能特性:

  • 完整支持 JSONata 语法。
  • 可访问整个工作流上下文。
  • 支持查询和聚合数据。

适用场景:

  • 将 API 响应映射到内部模型。
  • 过滤列表中的条目(例如 items[price > 50])。
  • 计算合计(例如 $sum(items.price))。

工作原理

  1. 解析配置:expr 中的 {{ }} 块先由 Expression Engine 求值;纯 JSONata 文本保持不变。
  2. expr 为空时失败。
  3. 若 expr 解析为对象或数组(整值 {{ }} 返回数据),则直接以该值为结果,跳过 JSONata。
  4. 否则将字符串编译为 JSONata 2,并以 payload 为根求值:order.total、$sum(items.price)、{ "id": order_id, "lines": $count(items) }。
  5. 通过 out 将结果作为新的 payload 返回。

在工作流中

节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。

{
  "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) }"
  }
}

配置

expr string 必填 支持表达式

要求值的 JSONata 表达式。其结果即为输出载荷。

配置中会显示通用执行设置,但该节点目前不执行重试(retryOnFail、maxTries)。所有节点均未实现 continueOnFail。

代码中的行为

除 schema 描述之外,实现代码对这些键的实际处理。

expr
JSONata 中没有绑定 Expression Engine 变量:$json.total 的结果为 undefined。请使用 payload 路径和 JSONata 内置函数($now()、$sum、$map),或在混合文本中用 {{ }} 嵌入引擎值(插入其字符串形式):{ "run": "{{ $execution.id }}", "total": $sum(items.price) }。混合文本中出错的 {{ }} 会在表达式里变成 [ERROR: <message>]。

输出端口

out 默认

表达式已求值。

数据
{
  "id": "ord_123",
  "total": 42.5,
  "lines": 2
}
error

expr 为空或 JSONata 出错。

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

在表达式中

后续节点可通过 $("Transform Data").item.json.<路径> 读取此节点输出中的内容(请把 Transform Data 换成你的节点标签)。Editor 标记工作流编辑器在 Previous Nodes 中提示的路径(输出契约);Worker 标记 worker 实际通过 out 返回的路径,依据“端口”一节所示的输出。

表达式EditorWorker
$("Transform Data").item.json.resultobjectworker 不返回:读取结果为 undefined —
$("Transform Data").item.json.idstring编辑器不提示= "ord_123"—
$("Transform Data").item.json.totalnumber编辑器不提示= 42.5—
$("Transform Data").item.json.linesnumber编辑器不提示= 2—

— Editor: 编辑器会提示它,但 worker 并不返回:读取结果为 undefined。

— Worker: worker 会返回它,但编辑器不会提示:请手动输入。对象内部的键可能取决于你的数据或配置(例如每个标签一个键)。

错误

消息 端口 原因与处理
Transformation expression is required error expr 缺失,或其 {{ }} 解析为空值。
Transform Error: <JSONata message> error JSONata 语法或运行时错误,例如 Unexpected end of expression。
Transform Error: path.charAt is not a function error 整值 {{ }} 返回了数字或布尔值,它既不是 JSONata 文本也不是数据。

输入契约

payload object 可选

输出契约

result object 可选

契约(JSON Schema)

与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。

{
  "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
    }
  }
}

执行

执行方式
sync
有状态
否
重试(retryOnFail)
否
能力
transformjsonatamappingutils
标签
transformjsonatamap

说明与限制

  • 若结果为 undefined(路径没有匹配)或假值,引擎会保存整个信封 { ok, port } 作为输出,而不是结果。
  • 之前的 payload 会被替换;如需之前的数据,请在混合文本中使用 {{ $("Node").item.json.field }}。
  • 当返回的信封没有 data(或 data 为假值)时,引擎会把整个信封作为节点输出:经 error 端口时,下一个节点收到的 payload 是 { ok: false, errors: [...], port: "error" }。

已对照编排器代码核实 · 31e874b · 2026-11-26

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

仅安装技能包,不会自动更新;需要时重新运行即可。

npx skills add https://docs.sommatic.ai
智能体如何读取本文档