JSONata functions
The built-in functions of JSONata, the language that data.transform and the expression fallback of flow nodes use.
Write JSONata without {{ }}; fields of the input are read by name. The signature uses JSONata notation (s string, n number, b boolean, a array, o object, f function, x any, - the input, ? optional).
$sum(items.price)Functions
$abs <n-:n>
Returns the absolute value of a number. If the argument is omitted, the context value is used. Example: $abs(-5) returns 5.
$append <xx:a>
Returns an array with the values of the first argument followed by the values of the second; a value that is not an array is treated as a single-element array. Example: $append([1,2,3], 4) returns [1,2,3,4].
$assert <bs?:x>
Throws an error with message when condition is false; when it is true, returns nothing (undefined). Example: $assert($count(items) > 0, "No items").
$average <a<n>:n>
Returns the mean of an array of numbers. Throws an error if any item is not a number. Example: $average([5,1,3,7,4]) returns 4.
$base64decode <s-:s>
Decodes a Base64 string into a UTF-8 string. Example: $base64decode("bXl1c2VyOm15cGFzcw==") returns "myuser:mypass".
$base64encode <s-:s>
Encodes a string as Base64, treating each character as one byte; characters outside the 0x00-0xFF range are not supported. Example: $base64encode("myuser:mypass") returns "bXl1c2VyOm15cGFzcw==".
$boolean <x-:b>
Casts a value to a Boolean: empty strings, 0, null, empty arrays and objects, arrays whose members all cast to false, and functions become false; other values become true.
$ceil <n-:n>
Rounds a number up to the nearest integer greater than or equal to it. Example: $ceil(5.3) returns 6.
$clone <(oa)-:o>
Returns a deep copy of an object or array, made by serializing it to JSON and parsing it back. It is not part of the documented function library; the transform operator | ... | ... | uses it to copy its input.
$contains <s-(sf):b>
Returns true if the string contains pattern, which can be a string (matched as contiguous characters) or a regular expression. Example: $contains("Hello World", /wo/i) returns true.
$count <a:n>
Returns the number of items in an array; a value that is not an array counts as one item. Example: $count([1,2,3,1]) returns 4.
$decodeUrl <s-:s>
Decodes a full URL encoded with $encodeUrl, turning percent-escape sequences back into characters.
$decodeUrlComponent <s-:s>
Decodes a URL component encoded with $encodeUrlComponent. Example: $decodeUrlComponent("%3Fx%3Dtest") returns "?x=test".
$distinct <x:x>
Returns an array with duplicate values removed, comparing values deeply as the = operator does. Example: $distinct([1,2,3,3,4,3,5]) returns [1,2,3,4,5].
$each <o-f:a>
Applies a function to each key/value pair of an object and returns an array with the results; the function receives the value and, optionally, the key. Example: $each(Address, function($v, $k) { $k & ": " & $v }).
$encodeUrl <s-:s>
Encodes a full URL by replacing characters that are not valid in a URL with UTF-8 percent-escape sequences, keeping its structural characters such as /, ? and =.
$encodeUrlComponent <s-:s>
Encodes a URL component by replacing reserved and non-ASCII characters with UTF-8 percent-escape sequences. Example: $encodeUrlComponent("?x=test") returns "%3Fx%3Dtest".
$error <s?:x>
Throws an error, with an optional message, which stops the evaluation. Example: $error("Invalid status").
$eval <sx?:x>
Parses and evaluates a string as JSON or as a JSONata expression, using the current context or the optional context argument. Example: $eval("[1,2,3]") returns [1,2,3].
$exists <x:b>
Returns true if the expression evaluates to a value and false if it matches nothing, such as a path to a missing field. Example: $exists(Account.Email).
$filter <af>
Returns the values of an array for which the function returns true; the function receives the value and, optionally, its index and the whole array. Example: $filter([1,2,3,4], function($v) { $v % 2 = 0 }) returns [2,4].
$floor <n-:n>
Rounds a number down to the nearest integer less than or equal to it. Example: $floor(-5.3) returns -6.
$formatBase <n-n?:s>
Converts a number to a string representing an integer in the given radix, from 2 to 36 (default 10). Example: $formatBase(100, 2) returns "1100100".
$formatInteger <n-s:s>
Formats a number as an integer string according to a picture string, following the XPath fn:format-integer syntax. Example: $formatInteger(1999, 'I') returns "MCMXCIX".
$formatNumber <n-so?:s>
Formats a number as a decimal string according to a picture string, following the XPath fn:format-number syntax; the optional options object overrides characters such as the decimal separator. Example: $formatNumber(12345.6, '#,###.00') returns "12,345.60".
$fromMillis <n-s?s?:s>
Converts milliseconds since the Unix epoch into a timestamp string: ISO 8601 by default, or formatted with an XPath fn:format-dateTime picture and an optional ±HHMM timezone. Example: $fromMillis(1510067557121, '[M01]/[D01]/[Y0001]') returns "11/07/2017".
$join <a<s>s?:s>
Joins an array of strings into a single string, with an optional separator (empty by default). Throws an error if any item is not a string. Example: $join(['a','b','c'], ', ') returns "a, b, c".
$keys <x-:a<s>>
Returns an array with the keys of an object; for an array of objects, returns the de-duplicated keys of all of them.
$length <s-:n>
Returns the number of characters in a string. Throws an error if the argument is not a string. Example: $length("Hello World") returns 11.
$lookup <x-s:x>
Returns the value stored under key in an object; for an array of objects, returns the values of every occurrence of key. Example: $lookup(Address, "City").
$lowercase <s-:s>
Converts all the characters of a string to lowercase. Example: $lowercase("Hello World") returns "hello world".
$map <af>
Returns an array with the result of applying a function to each value of an array; the function receives the value and, optionally, its index and the whole array. Example: $map([1..5], $string) returns ["1","2","3","4","5"].
$match <s-f<s:o>n?:a<o>>
Applies a regular expression to a string and returns an array of objects, one per match, with the fields match, index and groups; the optional limit caps the number of matches. Example: $match("ababbabbcc", /a(b+)/).
$max <a<n>:n>
Returns the largest number in an array of numbers. Throws an error if any item is not a number. Example: $max([5,1,3,7,4]) returns 7.
$merge <a<o>:o>
Merges an array of objects into a single object; when keys repeat, the value from the last object wins. Example: $merge([{"a":1},{"a":2,"b":3}]) returns {"a":2,"b":3}.
$min <a<n>:n>
Returns the smallest number in an array of numbers. Throws an error if any item is not a number. Example: $min([5,1,3,7,4]) returns 1.
$not <x-:b>
Casts the argument to a Boolean with the rules of $boolean and returns its negation. Example: $not(0) returns true.
$number <(nsb)-:n>
Casts a value to a number: numbers pass unchanged, strings holding a valid JSON number (or a 0x, 0o or 0b literal) are parsed, true becomes 1 and false becomes 0; any other value throws an error. Example: $number("5") returns 5.
$pad <s-ns?:s>
Pads a string to at least |width| characters with char (a space by default): a positive width pads on the right, a negative width on the left. Example: $pad("foo", -5, "#") returns "##foo".
$parseInteger <s-s:n>
Parses a string into an integer using a picture string with the same syntax as $formatInteger. Example: $parseInteger('12,345,678', '#,##0') returns 12345678.
$power <n-n:n>
Returns base raised to the power of exponent. Throws an error if the result cannot be represented as a JSON number. Example: $power(2, -2) returns 0.25.
$random <:n>
Returns a pseudo-random number greater than or equal to 0 and less than 1.
$reduce <afj?:j>
Combines the values of an array into one by applying a function successively; the function receives the accumulator and the current value, and the optional init sets the starting value (by default, the first item). Example: $reduce([1..5], function($acc, $v) { $acc * $v }) returns 120.
$replace <s-(sf)(sf)n?:s>
Replaces the occurrences of pattern (a string or a regular expression) in a string with replacement, which can be a string (with $1, $2... for captured groups) or a function; the optional limit caps the number of replacements. Example: $replace("John Smith and John Jones", "John", "Mr", 1) returns "Mr Smith and John Jones".
$reverse <a:a>
Returns an array with its values in reverse order. Example: $reverse(["Hello", "World"]) returns ["World", "Hello"].
$round <n-n?:n>
Rounds a number to the given number of decimal places (0 by default; a negative value rounds to the left of the decimal point), using round half to even. Example: $round(123.456, 2) returns 123.46.
$shuffle <a:a>
Returns an array with its values in random order. Example: $shuffle([1..9]).
$sift <o-f?:o>
Returns an object with only the key/value pairs for which the function returns true; the function receives the value and, optionally, the key and the whole object. Example: $sift(Product, function($v, $k) { $k ~> /^Product/ }).
$single <af?>
Returns the only value of an array that satisfies the function (or the only value, if no function is given). Throws an error if the number of matching values is not exactly one. Example: $single(Product, function($v) { $v.SKU = "0406654608" }).
$sort <af?:a>
Returns a sorted array: numbers in increasing order and strings by Unicode code point, or in the order set by an optional comparator function($l, $r) that returns true when $l must go after $r. The sort is stable. Example: $sort(Product, function($l, $r) { $l.Price > $r.Price }).
$split <s-(sf)n?:a<s>>
Splits a string into an array of substrings using a string or regular expression separator; the optional limit caps the number of substrings returned. Example: $split("so many words", " ", 2) returns ["so", "many"].
$spread <x-:a<o>>
Splits an object into an array of objects with one key/value pair each; for an array of objects, it does so for all of them. Example: $spread({"a":1,"b":2}) returns [{"a":1},{"b":2}].
$sqrt <n-:n>
Returns the square root of a number. Throws an error if the number is negative. Example: $sqrt(4) returns 2.
$string <x-b?:s>
Casts a value to a string: strings pass unchanged, functions become an empty string, and other values are serialized as JSON (pretty-printed if prettify is true). Infinity and NaN throw an error. Example: $string(5) returns "5".
$substring <s-nn?:s>
Returns the part of a string that starts at the zero-based position start (negative values count from the end), with at most length characters when given. Example: $substring("Hello World", 3, 5) returns "lo Wo".
$substringAfter <s-s:s>
Returns the part of a string after the first occurrence of chars, or the whole string if chars does not occur. Example: $substringAfter("Hello World", " ") returns "World".
$substringBefore <s-s:s>
Returns the part of a string before the first occurrence of chars, or the whole string if chars does not occur. Example: $substringBefore("Hello World", " ") returns "Hello".
$sum <a<n>:n>
Returns the sum of an array of numbers. Throws an error if any item is not a number. Example: $sum([5,1,3,7,4]) returns 20.
$toMillis <s-s?:n>
Converts a timestamp string into milliseconds since the Unix epoch. Without a picture the input must be ISO 8601; with one it is parsed using the XPath fn:format-dateTime syntax. Example: $toMillis("2017-11-07T15:07:54.972Z") returns 1510067274972.
$trim <s-:s>
Normalizes the whitespace of a string: replaces tabs, carriage returns and line feeds with spaces, collapses consecutive spaces into one, and removes leading and trailing spaces. Example: $trim(" Hello \n World ") returns "Hello World".
$type <x:s>
Returns the type of a value as a string: null, number, string, boolean, array, object or function; for an undefined value it returns undefined.
$uppercase <s-:s>
Converts all the characters of a string to uppercase. Example: $uppercase("Hello World") returns "HELLO WORLD".
$zip <a+>
Combines several arrays into an array of arrays grouped by position; the result is as long as the shortest input. Example: $zip([1,2,3], [4,5], [7,8,9]) returns [[1,4,7],[2,5,8]].