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

JSONata 函数

JSONata 的内置函数。data.transform 以及流程控制节点的备用表达式模式都使用 JSONata。

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

版本 2.1.0 MIT 官方文档

编写 JSONata 时不要加 {{ }},直接按字段名读取输入。签名采用 JSONata 记法(s 字符串、n 数字、b 布尔、a 数组、o 对象、f 函数、x 任意类型、- 输入值、? 可选)。

$sum(items.price)

函数

$abs <n-:n>

返回数字的绝对值;省略参数时作用于上下文值。例:$abs(-5) 返回 5。

$abs(-4.5)
结果 number
4.5

$append <xx:a>

按顺序连接两个数组;非数组参数会先视为单元素数组。例:$append([1,2,3], 4) 返回 [1,2,3,4]。

$append(body.tags, 'priority')
结果 list
["new","web","new","priority"]

$assert <bs?:x>

条件为 true 时返回 undefined;为 false 时以 message 抛出错误,适合做输入校验。例:$assert(Price > 0, "Price must be positive")。

$assert($count(body.items) > 0, 'The order has no items')
结果 undefined
undefined

$average <a<n>:n>

返回数字数组的平均值;数组含非数字元素时报错。例:$average([5,1,3,7,4]) 返回 4。

$average(body.items.price)
结果 number
13.166666666666666

$base64decode <s-:s>

把 Base64 字符串解码为字符串(按 UTF-8 解码)。例:$base64decode("bXl1c2VyOm15cGFzcw==") 返回 "myuser:mypass"。

$base64decode('U29tbWF0aWM=')
结果 string
"Sommatic"

$base64encode <s-:s>

把字符串编码为 Base64,每个字符视为一个字节,字符须在 0x00 到 0xFF 范围内。例:$base64encode("myuser:mypass") 返回 "bXl1c2VyOm15cGFzcw=="。

$base64encode(body.order_id)
结果 string
"b3JkXzEwNDI="

$boolean <x-:b>

按 JSONata 规则把值转换为布尔值:空字符串、0、null、空数组、空对象和函数为 false,非空字符串、非零数字、非空对象为 true;数组只要有一个成员转换为 true 即为 true。例:$boolean("") 返回 false。

$boolean(body.priority)
结果 boolean
false

$ceil <n-:n>

向上取整,返回大于或等于该数的最小整数;省略参数时作用于上下文值。例:$ceil(-5.3) 返回 -5。

$ceil(body.items[0].price)
结果 number
11

$clone <(oa)-:o>

返回参数的深拷贝(经 JSON 序列化再解析),修改副本不会影响原值;变换运算符 |...| 内部即用它复制输入。例:$clone({"a": [1]}) 返回新的 {"a": [1]}。

$clone(body.customer)
结果 object
{"name":"Ana Pérez","segment":"gold"}

$contains <s-(sf):b>

判断字符串 str 中是否包含 pattern,pattern 可为字符串或正则表达式。例:$contains("abracadabra", /a.*a/) 返回 true。

$contains(body.email, '@example.com')
结果 boolean
true

$count <a:n>

返回数组的元素个数;非数组值视为单元素数组(结果为 1),省略参数时作用于上下文值。例:$count([1,2,3,1]) 返回 4。

$count(body.items)
结果 number
3

$decodeUrl <s-:s>

对完整 URL 中的百分号编码进行解码,是 $encodeUrl 的逆操作。例:$decodeUrl("https://mozilla.org/?x=%D1%88") 返回 "https://mozilla.org/?x=ш"。

$decodeUrl('https://example.com/a%20b')
结果 string
"https://example.com/a b"

$decodeUrlComponent <s-:s>

对 URL 组成部分中的百分号编码进行解码,是 $encodeUrlComponent 的逆操作。例:$decodeUrlComponent("%3Fx%3Dtest") 返回 "?x=test"。

$decodeUrlComponent('Ana%20P%C3%A9rez')
结果 string
"Ana Pérez"

$distinct <x:x>

返回去重后的数组副本,按深度相等(与 = 运算符相同)比较。例:$distinct([1,2,3,3,4,3,5]) 返回 [1,2,3,4,5]。

$distinct(body.tags)
结果 list
["new","web"]

$each <o-f:a>

对对象的每个键值对调用 function(value, key),返回结果组成的数组。例:$each(Address, function($v, $k) {$k & ": " & $v})。

$each(body.customer, function($value, $key) { $key & '=' & $value })
结果 list
["name=Ana Pérez","segment=gold"]

$encodeUrl <s-:s>

对完整 URL 中需要转义的字符进行 UTF-8 百分号编码,保留 :、/、? 等 URL 结构字符。例:$encodeUrl("https://mozilla.org/?x=ш") 返回 "https://mozilla.org/?x=%D1%88"。

$encodeUrl('https://example.com/search?q=Ana Pérez')
结果 string
"https://example.com/search?q=Ana%20P%C3%A9rez"

$encodeUrlComponent <s-:s>

对 URL 组成部分(如查询参数值)进行 UTF-8 百分号编码,?、=、& 等也会被编码。例:$encodeUrlComponent("?x=test") 返回 "%3Fx%3Dtest"。

$encodeUrlComponent(body.customer.name)
结果 string
"Ana%20P%C3%A9rez"

$error <s?:x>

主动抛出错误,可附带说明信息 message,用于中止求值。例:$error("Invalid input")。

$exists(body.shipping) ? body.shipping : $error('Missing shipping')
抛出
Missing shipping

$eval <sx?:x>

把字符串当作 JSON 或 JSONata 表达式解析并求值;可选的第二个参数指定求值上下文。例:$eval("[1,2,3]") 返回 [1, 2, 3]。

$eval('[1, 2, 3]')
结果 list
[1,2,3]

$exists <x:b>

表达式求值得到任意值时返回 true,未匹配到任何值(如路径指向不存在的字段)时返回 false。例:输入中没有 Phone 字段时,$exists(Phone) 返回 false。

$exists(body.shipping)
结果 boolean
false

$filter <af>

返回数组中使 function(value [, index [, array]]) 返回 true 的元素。例:$filter([1..6], function($v) { $v % 2 = 0 }) 返回 [2, 4, 6]。

$filter(body.items, function($item) { $item.qty > 0 }).sku
结果 list
["A-100","C-300"]

$floor <n-:n>

向下取整,返回小于或等于该数的最大整数;省略参数时作用于上下文值。例:$floor(-5.3) 返回 -6。

$floor(body.items[0].price)
结果 number
10

$formatBase <n-n?:s>

把数字转换为以 radix 进制表示的整数字符串,radix 取 2 到 36,默认 10。例:$formatBase(100, 2) 返回 "1100100"。

$formatBase(255, 16)
结果 string
"ff"

$formatInteger <n-s:s>

按 XPath fn:format-integer 的 picture 语法把数字格式化为整数字符串(支持罗马数字、英文单词等)。例:$formatInteger(1999, 'I') 返回 "MCMXCIX"。

$formatInteger(1042, '000000')
结果 string
"001042"

$formatNumber <n-so?:s>

按 XPath fn:format-number 的 picture 语法把数字格式化为字符串;可选 options 对象覆盖小数点、千位分隔符等字符。例:$formatNumber(12345.6, '#,###.00') 返回 "12,345.60"。

$formatNumber(1234.5, '#,##0.00')
结果 string
"1,234.50"

$fromMillis <n-s?s?:s>

把自纪元以来的毫秒数转换为时间字符串:默认输出 ISO 8601;提供 picture 时按 XPath fn:format-dateTime 语法格式化,timezone 为 ±HHMM 形式的偏移。例:$fromMillis(1510067557121) 返回 "2017-11-07T15:12:37.121Z"。

$fromMillis(1795703400000)
结果 string
"2026-11-26T14:30:00.000Z"
$fromMillis(1795703400000, '[Y0001]-[M01]-[D01]')
结果 string
"2026-11-26"

$join <a<s>s?:s>

把字符串数组拼接成一个字符串,可选分隔符默认为空串;元素或分隔符不是字符串时报错。例:$join(['a','b','c'], '-') 返回 "a-b-c"。

$join(body.items.sku, ', ')
结果 string
"A-100, B-200, C-300"

$keys <x-:a<s>>

返回对象的键名数组;参数为对象数组时,返回所有对象键名去重后的数组。例:$keys({"a":1,"b":2}) 返回 ["a", "b"]。

$keys(body.customer)
结果 list
["name","segment"]

$length <s-:n>

返回字符串的字符数;参数不是字符串时报错。例:$length("Hello World") 返回 11。

$length(body.order_id)
结果 number
8

$lookup <x-s:x>

返回对象中 key 对应的值;参数为对象数组时,返回所有对象中该键的值。例:$lookup({"a":1,"b":2}, "a") 返回 1。

$lookup(body.customer, 'segment')
结果 string
"gold"

$lowercase <s-:s>

把字符串的所有字符转为小写。例:$lowercase("Hello World") 返回 "hello world"。

$lowercase(body.customer.name)
结果 string
"ana pérez"

$map <af>

对数组每个元素调用 function(value [, index [, array]]),按原顺序返回结果数组。例:$map([1..3], $string) 返回 ["1", "2", "3"]。

$map(body.items, function($item) { $item.qty * $item.price })
结果 list
[21,0,25]

$match <s-f<s:o>n?:a<o>>

用正则表达式匹配字符串,返回匹配对象数组,每项含 match、index 和 groups(捕获组);可选 limit 限制匹配次数。例:$match("ababbabbcc", /a(b+)/) 的第一项为 {"match": "ab", "index": 0, "groups": ["b"]}。

$match(body.order_id, /\d+/).match
结果 string
"1042"

$max <a<n>:n>

返回数字数组中的最大值;数组含非数字元素时报错。例:$max([5,1,3,7,4]) 返回 7。

$max(body.items.price)
结果 number
25

$merge <a<o>:o>

把对象数组合并为一个对象,键重复时以后面对象的值为准;数组中有非对象元素时报错。例:$merge([{"a":1},{"b":2,"a":3}]) 返回 {"a": 3, "b": 2}。

$merge([body.customer, { 'vip': true }])
结果 object
{"name":"Ana Pérez","segment":"gold","vip":true}

$min <a<n>:n>

返回数字数组中的最小值;数组含非数字元素时报错。例:$min([5,1,3,7,4]) 返回 1。

$min(body.items.qty)
结果 number
0

$not <x-:b>

先按 $boolean 规则把参数转换为布尔值,再取反。例:$not(0) 返回 true。

$not($exists(body.shipping))
结果 boolean
true

$number <(nsb)-:n>

把值转换为数字:数字原样返回,合法的 JSON 数字字符串被转换,true 为 1、false 为 0,其他值报错。例:$number("5") 返回 5。

$number('42.5')
结果 number
42.5

$pad <s-ns?:s>

把字符串填充到至少 |width| 个字符:width 为正数时在右侧填充,为负数时在左侧填充,默认填充字符为空格。例:$pad("foo", -5, "#") 返回 "##foo"。

$pad(body.order_id, 12, '*')
结果 string
"ord_1042****"

$parseInteger <s-s:n>

按与 $formatInteger 相同的 picture 语法把字符串解析为整数(JSONata 扩展)。例:$parseInteger('12,345,678', '#,##0') 返回 12345678。

$parseInteger('001,042', '#,##0')
结果 number
1042

$power <n-n:n>

返回 base 的 exponent 次幂;结果无法表示为 JSON 数字(如无穷大或复数)时报错。例:$power(2, 8) 返回 256。

$power(2, 10)
结果 number
1024

$random <:n>

返回一个伪随机数 n,满足 0 ≤ n < 1。例:$floor($random() * 10) 得到 0 到 9 之间的随机整数。

$random()
结果 number 随机:每次运行都会生成新值
0.5850853985132143

$reduce <afj?:j>

把数组归约为单个值:依次以 function(accumulator, value [, index [, array]]) 处理元素;省略 init 时以第一个元素作为初始累加值。例:$reduce([1..5], function($i, $j){$i * $j}) 返回 120。

$reduce(body.items.qty, function($sum, $qty) { $sum + $qty }, 0)
结果 number
3

$replace <s-(sf)(sf)n?:s>

把字符串中匹配 pattern(字符串或正则)的部分替换为 replacement;替换值可为字符串(正则中支持 $1 等分组引用)或返回字符串的函数,可选 limit 限制替换次数。例:$replace("John Smith and John Jones", "John", "Mr", 1) 返回 "Mr Smith and John Jones"。

$replace(body.email, '@example.com', '')
结果 string
"ana.perez"

$reverse <a:a>

返回元素顺序反转后的数组副本。例:$reverse(["Hello", "World"]) 返回 ["World", "Hello"]。

$reverse(body.items.sku)
结果 list
["C-300","B-200","A-100"]

$round <n-n?:n>

按 precision 位小数四舍五入(默认 0,负数表示对小数点左侧取整),恰好处于中间值时采用“银行家舍入”(取偶数)。例:$round(123.456, 2) 返回 123.46,$round(2.5) 返回 2。

$round(10.567, 2)
结果 number
10.57

$shuffle <a:a>

返回元素顺序随机打乱后的数组副本。例:$shuffle([1..9]) 返回 1 到 9 的随机排列。

$shuffle(body.items.sku)
结果 list 随机:每次运行都会生成新值
["A-100","C-300","B-200"]

$sift <o-f?:o>

返回只包含使 function(value [, key [, object]]) 返回 true 的键值对的新对象;省略对象参数时作用于上下文值。例:$sift(Product, function($v, $k) {$k ~> /^Product/}) 只保留以 Product 开头的字段。

$sift(body.customer, function($value) { $value != 'gold' })
结果 object
{"name":"Ana Pérez"}

$single <af?>

返回数组中唯一使 function(value [, index [, array]]) 返回 true 的元素;匹配数不是恰好一个(零个或多个)时报错。例:$single(Product, function($v) { $v.SKU = "0406654608" })。

$single(body.items, function($item) { $item.sku = 'C-300' }).price
结果 number
25

$sort <af?:a>

返回排序后的数组副本:不传比较函数时数组只能全为数字或全为字符串;传入 function(left, right) 时,返回 true 表示交换两者。排序是稳定的。例:$sort([3,1,2]) 返回 [1,2,3]。

$sort(body.items, function($left, $right) { $left.price > $right.price }).sku
结果 list
["B-200","A-100","C-300"]

$split <s-(sf)n?:a<s>>

按分隔符(字符串或正则)把字符串拆分为数组;空分隔符拆成单个字符,可选 limit 限制结果个数。例:$split("so many words", " ", 2) 返回 ["so", "many"]。

$split(body.email, '@')
结果 list
["ana.perez","example.com"]

$spread <x-:a<o>>

把对象拆分为由单个键值对对象组成的数组;参数为对象数组时,对所有对象的每个键值对各生成一项。例:$spread({"a":1,"b":2}) 返回 [{"a":1}, {"b":2}]。

$spread(body.customer)
结果 list
[{"name":"Ana Pérez"},{"segment":"gold"}]

$sqrt <n-:n>

返回数字的平方根;参数为负数时报错。例:$sqrt(4) 返回 2。

$sqrt(16)
结果 number
4

$string <x-b?:s>

把值转换为字符串:字符串原样返回,函数转换为空串,NaN 与无穷大报错,其他值序列化为 JSON;prettify 为 true 时输出带缩进。例:$string(5) 返回 "5"。

$string(body.items[0])
结果 string
"{\"sku\":\"A-100\",\"qty\":2,\"price\":10.5}"

$substring <s-nn?:s>

从从零开始的位置 start 截取最多 length 个字符;start 为负数时从末尾倒数。例:$substring("Hello World", 3, 5) 返回 "lo Wo"。

$substring(body.order_id, 4)
结果 string
"1042"

$substringAfter <s-s:s>

返回字符串中第一次出现 chars 之后的部分;不包含 chars 时原样返回字符串。例:$substringAfter("Hello World", " ") 返回 "World"。

$substringAfter(body.email, '@')
结果 string
"example.com"

$substringBefore <s-s:s>

返回字符串中第一次出现 chars 之前的部分;不包含 chars 时原样返回字符串。例:$substringBefore("Hello World", " ") 返回 "Hello"。

$substringBefore(body.email, '@')
结果 string
"ana.perez"

$sum <a<n>:n>

返回数字数组的总和;数组含非数字元素时报错。例:$sum([5,1,3,7,4]) 返回 20。

$sum(body.items.(qty * price))
结果 number
46

$toMillis <s-s?:n>

把时间字符串转换为自纪元以来的毫秒数:默认要求 ISO 8601 格式,提供 picture 时按 XPath fn:format-dateTime 语法解析。例:$toMillis("2017-11-07T15:07:54.972Z") 返回 1510067274972。

$toMillis(body.created_at)
结果 number
1795598100000

$trim <s-:s>

规范化空白:把制表符、回车和换行转为空格,将连续空格合并为一个,并去除首尾空格。例:$trim(" Hello \n World ") 返回 "Hello World"。

$trim('  Ana   Pérez  ')
结果 string
"Ana Pérez"

$type <x:s>

返回值的类型名:"null"、"number"、"string"、"boolean"、"array"、"object" 或 "function";参数为 undefined 时返回 undefined。例:$type([1,2]) 返回 "array"。

$type(body.items)
结果 string
"array"

$uppercase <s-:s>

把字符串的所有字符转为大写。例:$uppercase("Hello World") 返回 "HELLO WORLD"。

$uppercase(body.order_id)
结果 string
"ORD_1042"

$zip <a+>

接受任意多个数组,按索引把各数组的元素分组;结果长度等于最短数组的长度。例:$zip([1,2,3], [4,5], [7,8,9]) 返回 [[1,4,7], [2,5,8]]。

$zip(body.items.sku, body.items.qty)
结果 list
[["A-100",2],["B-200",0],["C-300",1]]

安装智能体工具

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

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

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