# Helpers

> Defaults for empty values, slugs, JMESPath queries and lodash.

Source: https://docs.sommatic.ai/en/expressions/reference/helpers/

Each example was run against the deployed expression engine over the [example context](#example-context) at the end of the page.

## `$ifEmpty`

Function · `any` · Source: `Sommatic AI expression engine`

```javascript
$ifEmpty(value, fallback)
```

Returns `fallback` when `value` is `undefined`, `null`, an empty string `''`, an empty list or an object without keys. Any other value is returned as it is: `0`, `false` and a string of spaces are kept.

### Parameters

- `value` (any, required): The value to check, usually a read that may be missing.
- `fallback` (any, required): What to return when `value` is empty. Any type.

### Returns

`any`: `value` or `fallback`, with its own type.

### Examples

An empty string takes the fallback.

```javascript
{{ $ifEmpty($json.body.priority, 'normal') }}
```

Result (string):

```json
"normal"
```

```javascript
{{ $ifEmpty($json.body.customer.phone, 'no phone') }}
```

Result (string):

```json
"no phone"
```

A node that did not run.

```javascript
{{ $ifEmpty($('Approval').item.json.decision, 'pending') }}
```

Result (string):

```json
"pending"
```

0 is not empty.

```javascript
{{ $ifEmpty(0, 10) }}
```

Result (number):

```json
0
```

```javascript
{{ $ifEmpty([], ['none']) }}
```

Result (list):

```json
[
  "none"
]
```

### Notes

- To also replace `0` or `false`, use `||`: `{{ $json.body.retries || 3 }}`. To replace only `null` and `undefined`, use `??`.

In JSONata: `$$ifEmpty(body.priority, "normal")`

## `$slugify`

Function · `string` · Source: `lodash · kebabCase`

```javascript
$slugify(text)
```

Turns any text into kebab case (lodash `kebabCase`): lowercase words joined by hyphens, accents removed, punctuation dropped. Words are also split where the case changes and between letters and digits, so `helloWorld ABC123` becomes `hello-world-abc-123`.

### Parameters

- `text` (any, required): Converted to text first: `null` becomes `'null'`.

### Returns

`string`: The slug; an empty string for empty text.

### Examples

```javascript
{{ $slugify('Hola Mundo Ñandú 2026') }}
```

Result (string):

```json
"hola-mundo-nandu-2026"
```

```javascript
{{ $slugify($json.body.customer.name) }}
```

Result (string):

```json
"ana-perez"
```

```javascript
{{ $slugify('helloWorld ABC123') }}
```

Result (string):

```json
"hello-world-abc-123"
```

### Notes

- Guard missing values first: `$slugify($ifEmpty($json.title, 'untitled'))`.

In JSONata: `$$slugify(body.customer.name)`

## `$jmespath`

Function · `any` · Source: `jmespath · search`

```javascript
$jmespath(data, query)
```

Runs a [JMESPath](https://docs.sommatic.ai/en/expressions/jmespath/) query over `data` and returns what it selects: a value, a list or an object. A query is often shorter than the JavaScript for deep responses: filters (`[?qty > \`0\`]`), projections (`[*].sku`) and functions (`length`, `sort_by`, `max_by`). Literals go between backticks inside the query.

### Parameters

- `data` (any, required): The value to query, usually `$json` or a node's output.
- `query` (string, required): A JMESPath expression. Write it between single quotes so the backticks and double quotes inside it need no escaping.

### Returns

`any`: What the query selects; `null` when nothing matches.

### Examples

```javascript
{{ $jmespath($json.body, 'items[?qty > `0`].sku') }}
```

Result (list):

```json
[
  "A-100",
  "C-300"
]
```

```javascript
{{ $jmespath($json.body, 'length(items)') }}
```

Result (number):

```json
3
```

```javascript
{{ $jmespath($json.body, 'max_by(items, &price).sku') }}
```

Result (string):

```json
"C-300"
```

```javascript
{{ $jmespath($json.body, 'items[*].{ id: sku, units: qty }') }}
```

Result (list):

```json
[
  {
    "id": "A-100",
    "units": 2
  },
  {
    "id": "B-200",
    "units": 0
  },
  {
    "id": "C-300",
    "units": 1
  }
]
```

A path that does not exist gives null, not an error.

```javascript
{{ $jmespath($json.body, 'shipping.address.city') }}
```

Result (null):

```json
null
```

A literal without backticks is a syntax error.

```javascript
{{ $jmespath($json.body, 'items[?qty > 0]') }}
```

Throws:

```text
Invalid token (Number): "0"
```

### Errors

- `Invalid token (<type>): "<value>"`: The query is not valid JMESPath, often a number or a string literal without backticks.

In JSONata: `$$jmespath($$json, "body.items[0].sku")`

## `_`

Library · `lodash` · Source: `lodash`

```javascript
_
```

The full [lodash](https://docs.sommatic.ai/en/expressions/lodash/) library. It is the shortest way to group, sum, pick, deduplicate and read deep paths with a default.

### Examples

```javascript
{{ _.sumBy($json.body.items, 'qty') }}
```

Result (number):

```json
3
```

```javascript
{{ _.uniq($json.body.tags) }}
```

Result (list):

```json
[
  "new",
  "web"
]
```

```javascript
{{ _.get($json, 'body.shipping.city', 'unknown') }}
```

Result (string):

```json
"unknown"
```

```javascript
{{ _.pick($json.body, ['order_id', 'email']) }}
```

Result (object):

```json
{
  "order_id": "ord_1042",
  "email": "ana.perez@example.com"
}
```

```javascript
{{ _.keyBy($json.body.items, 'sku') }}
```

Result (object):

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

### Notes

- Every function with its parameters, return value and examples is in the [lodash reference](https://docs.sommatic.ai/en/expressions/lodash/).

In JSONata: Not available in JSONata, which has its own function library.

## Example context

The examples run over this data: the input of the current node (`$json`), the nodes that already ran (`$(…)`), the ids of the run and the current node. The clock is fixed at `2026-11-26T14:30:00.000Z` and the server zone is UTC, so `$now` and `$today` give the same values on every page.

```json
{
  "$json": {
    "body": {
      "order_id": "ord_1042",
      "email": "ana.perez@example.com",
      "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"
  },
  "nodes": {
    "Webhook Receiver": "= $json",
    "Normalize input": {
      "correlation_id": "c-7f3a",
      "channel": "web"
    },
    "Classify": {
      "label": "billing",
      "confidence": 0.92
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-xxxx",
    "$flow_instance_id": "fi-xxxx",
    "$flow_definition_id": "fd-xxxx",
    "$flow_version_id": "fv-xxxx",
    "$flow_slug": "order-intake"
  },
  "node": {
    "node_id": "node-notify",
    "label": "Notify customer"
  }
}
```

The examples of a chained workflow run over a second context: a workflow started by `workflow.chain`, whose `workflow.trigger` node is labelled `Chain Entry Point`.

```json
{
  "$json": {
    "order_id": "ord_1042",
    "customer_email": "ana.perez@example.com",
    "amount": 46,
    "chain_context": {
      "caller_flow_definition_id": "fd-caller-xxxx",
      "caller_flow_instance_id": "fi-caller-xxxx",
      "caller_flow_slug": "order-intake",
      "caller_node_id": "node-chain-billing",
      "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
      "root_flow_instance_result_id": "fir-root-xxxx"
    }
  },
  "run": {
    "$flow_instance_result_id": "fir-hop-xxxx",
    "$flow_instance_id": "fi-hop-xxxx",
    "$flow_definition_id": "fd-billing-xxxx",
    "$flow_version_id": "fv-billing-xxxx",
    "$flow_slug": "billing-review",
    "$input": {
      "data": {
        "order_id": "ord_1042",
        "customer_email": "ana.perez@example.com",
        "amount": 46
      },
      "chain_context": {
        "caller_flow_definition_id": "fd-caller-xxxx",
        "caller_flow_instance_id": "fi-caller-xxxx",
        "caller_flow_slug": "order-intake",
        "caller_node_id": "node-chain-billing",
        "correlation_id": "chain-7Hq2Lw9sKd3pXa1mZr4t",
        "root_flow_instance_result_id": "fir-root-xxxx"
      }
    }
  },
  "node": {
    "node_id": "node-create-task",
    "label": "Create review task"
  }
}
```

Examples run against the orchestrator at commit 31e874b.
