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

Helpers

空值的默认值、slug、JMESPath 查询与 lodash。

以 Markdown 查看
在以下应用中打开

每个示例都在已部署的表达式引擎上、基于页面末尾的示例上下文运行过。

$ifEmpty

$ifEmpty(value, fallback)

当 value 为 undefined、null、空字符串 ''、空列表或没有键的对象时,返回 fallback。其他值原样返回:0、false 和全是空格的字符串都会保留。

参数

value any 必填
要检查的值,通常是可能缺失的读取结果。
fallback any 必填
value 为空时返回的值。可为任意类型。

返回

any value 或 fallback,保留各自的类型。

示例

空字符串会取 fallback。

{{ $ifEmpty($json.body.priority, 'normal') }}
结果 string
"normal"
{{ $ifEmpty($json.body.customer.phone, 'no phone') }}
结果 string
"no phone"

未执行的节点。

{{ $ifEmpty($('Approval').item.json.decision, 'pending') }}
结果 string
"pending"

0 不算空。

{{ $ifEmpty(0, 10) }}
结果 number
0
{{ $ifEmpty([], ['none']) }}
结果 list
["none"]

说明

  • 若还要替换 0 或 false,请用 ||:{{ $json.body.retries || 3 }}。若只替换 null 和 undefined,请用 ??。

在 JSONata 中 $$ifEmpty(body.priority, "normal")

$slugify

$slugify(text)

将任意文本转换为 kebab case(lodash 的 kebabCase):小写单词以连字符连接,去除重音符号,删除标点。在大小写变化处以及字母与数字之间也会拆分单词,因此 helloWorld ABC123 会变为 hello-world-abc-123。

参数

text any 必填
先转换为文本:null 变为 'null'。

返回

string slug;文本为空时返回空字符串。

示例

{{ $slugify('Hola Mundo Ñandú 2026') }}
结果 string
"hola-mundo-nandu-2026"
{{ $slugify($json.body.customer.name) }}
结果 string
"ana-perez"
{{ $slugify('helloWorld ABC123') }}
结果 string
"hello-world-abc-123"

说明

  • 先处理缺失值:$slugify($ifEmpty($json.title, 'untitled'))。

在 JSONata 中 $$slugify(body.customer.name)

$jmespath

$jmespath(data, query)

对 data 执行 JMESPath 查询,返回其选中的内容:一个值、列表或对象。对于层级较深的响应,查询往往比等效的 JavaScript 更简短:过滤([?qty > \0`])、投影([*].sku)和函数(length、sort_by、max_by`)。查询中的字面量要放在反引号之间。

参数

data any 必填
要查询的值,通常是 $json 或某个节点的输出。
query string 必填
JMESPath 表达式。请用单引号括起来,这样其中的反引号和双引号无需转义。

返回

any 查询选中的内容;没有匹配时为 null。

示例

{{ $jmespath($json.body, 'items[?qty > `0`].sku') }}
结果 list
["A-100","C-300"]
{{ $jmespath($json.body, 'length(items)') }}
结果 number
3
{{ $jmespath($json.body, 'max_by(items, &price).sku') }}
结果 string
"C-300"
{{ $jmespath($json.body, 'items[*].{ id: sku, units: qty }') }}
结果 list
[
  {
    "id": "A-100",
    "units": 2
  },
  {
    "id": "B-200",
    "units": 0
  },
  {
    "id": "C-300",
    "units": 1
  }
]

不存在的路径返回 null,而不是报错。

{{ $jmespath($json.body, 'shipping.address.city') }}
结果 null
null

不带反引号的字面量是语法错误。

{{ $jmespath($json.body, 'items[?qty > 0]') }}
抛出
Invalid token (Number): "0"

错误

消息 原因与解决方法
Invalid token (<type>): "<value>" 查询不是有效的 JMESPath,常见原因是数字或字符串字面量没有加反引号。

在 JSONata 中 $$jmespath($$json, "body.items[0].sku")

_

_

完整的 lodash 库。它是分组、求和、挑选字段、去重以及带默认值读取深层路径的最简洁方式。

示例

{{ _.sumBy($json.body.items, 'qty') }}
结果 number
3
{{ _.uniq($json.body.tags) }}
结果 list
["new","web"]
{{ _.get($json, 'body.shipping.city', 'unknown') }}
结果 string
"unknown"
{{ _.pick($json.body, ['order_id', 'email']) }}
结果 object
{"order_id":"ord_1042","email":"[email protected]"}
{{ _.keyBy($json.body.items, 'sku') }}
结果 object
{
  "A-100": {
    "sku": "A-100",
    "qty": 2,
    "price": 10.5
  },
  "B-200": {
    "sku": "B-200",
    "qty": 0,
    "price": 4
  },
  "C-300": {
    "sku": "C-300",
    "qty": 1,
    "price": 25
  }
}

说明

在 JSONata 中 JSONata 中不可用,它有自己的函数库。

示例上下文

示例基于以下数据运行:当前节点的输入($json)、已运行的节点($(…))、本次运行的 ID 以及当前节点。时钟固定为 2026-11-26T14:30:00.000Z,服务器时区为 UTC,因此各页面中 $now 与 $today 的值都相同。

当前节点的输入 JSON
{
  "$json": {
    "body": {
      "order_id": "ord_1042",
      "email": "[email protected]",
      "customer": {
        "name": "Ana Pérez",
        "segment": "gold"
      },
      "priority": "",
      "items": [
        {
          "sku": "A-100",
          "qty": 2,
          "price": 10.5
        },
        {
          "sku": "B-200",
          "qty": 0,
          "price": 4
        },
        {
          "sku": "C-300",
          "qty": 1,
          "price": 25
        }
      ],
      "tags": [
        "new",
        "web",
        "new"
      ],
      "created_at": "2026-11-25T09:15:00Z"
    },
    "headers": {
      "content-type": "application/json"
    },
    "query": {},
    "method": "POST"
  }
}
已运行的节点(标签 → 输出) JSON
{
  "Webhook Receiver": "= $json",
  "Normalize input": {
    "correlation_id": "c-7f3a",
    "channel": "web"
  },
  "Classify": {
    "label": "billing",
    "confidence": 0.92
  }
}
运行与当前节点 JSON
{
  "$execution": {
    "id": "fir-xxxx",
    "flow_instance_id": "fi-xxxx",
    "flow_definition_id": "fd-xxxx",
    "flow_version_id": "fv-xxxx",
    "flow_slug": "order-intake"
  },
  "$node": {
    "id": "node-notify",
    "label": "Notify customer"
  }
}

示例运行所基于的编排器提交 31e874b

安装智能体工具

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

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

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