Transform Reference
Array Verbs
Array indexing, filtering, sorting, deduplication, and transformation.
| Verb | Syntax | Description | Try It |
|---|---|---|---|
| at | %at @array index | Get element at index | Try |
| filter | %filter @array "condition" | Filter by condition | Try |
| flatten | %flatten @array | Flatten nested arrays | Try |
| distinct | %distinct @array | Unique values only | Try |
| sort | %sort @array | Sort ascending | Try |
| sortBy | %sortBy @array "field" | Sort by field | Try |
| reverse | %reverse @array | Reverse order | Try |
| reduce | %reduce @array "verb" initialValue | Fold array to single value using named verb | Try |
| pivot | %pivot @array "keyField" "valueField" | Array of objects to object keyed by field | Try |
| unpivot | %unpivot @object "keyName" "valueName" | Object to array of {key, value} objects | Try |
| intersection | %intersection @a @b | Distinct elements in both arrays | Try |
| union | %union @a @b | Distinct elements from both arrays | Try |
| difference | %difference @a @b | Distinct elements of the first not in the second | Try |
| symmetricDifference | %symmetricDifference @a @b | Distinct elements in exactly one array | Try |
| countBy | %countBy @array ["field"] | Count items per field value (keys sorted) | Try |
| keyBy | %keyBy @array "field" | Index items by a field value (last wins) | Try |
| explode | %explode @array "field" | One row per element of an array-valued field | Try |
| window | %window @array n | Overlapping consecutive slices of length n | Try |
Reference
Reduce (reduce) - Fold an Array
Applies a named verb cumulatively to array elements, reducing to a single value.
%reduce @array "verbName" initialValue
- The verb must accept 2 arguments (accumulator, current element).
- Compatible verbs:
add,subtract,multiply,divide,concat,minOf,maxOf, and any custom 2-argument verb. - If the array is empty, returns the initial value.
- Null elements are skipped.
Pivot (pivot) - Array to Object
Transforms an array of objects into a single object, using one field as keys and another as values.
%pivot @array "keyField" "valueField"
- Each object in the array must have both fields. Objects missing either field are skipped.
- If duplicate keys exist, the last value wins.
- Key values are coerced to strings.
- Returns null if input is not an array.
Example:
Input: [{"k":"width","v":100}, {"k":"height","v":200}]
Output: {"width": 100, "height": 200}
Unpivot (unpivot) - Object to Array
Transforms an object into an array of {key, value} objects.
%unpivot @object "keyName" "valueName"
- Each property of the input object becomes one element in the output array.
- Field names in the output are controlled by the
keyNameandvalueNameparameters. - Property order follows the original object's key order.
- Returns null if input is not an object.
Example:
Input: {"width": 100, "height": 200}
Output: [{"name":"width","size":100}, {"name":"height","size":200}]Examples
Array verb examples
second_vehicle = %at @vehicles ##1
active_coverages = %filter @coverages "@.status = 'active'"
unique_states = %distinct @addresses.state
by_premium = %sortBy @coverages "premium"
; Reduce - fold array with a verb
total = %reduce @prices "add" ##0 ; sum all prices
product = %reduce @factors "multiply" ##1 ; multiply all
; Pivot - array of objects to keyed object
settings = %pivot @configs "key" "value" ; [{key:"a",value:1}] -> {a:1}
; Unpivot - object to array of {key, value}
rows = %unpivot @dimensions "name" "size" ; {w:100,h:200} -> [{name:"w",size:100},...]