# JSONata expressions

FetchCatch evaluates **JSONata** expressions in condition, transform, wait_for_event, HTTP input mappings, and decision `responseValues`.

Library: [JSONata](https://jsonata.org/) via `Jsonata.Net.Native`.

## Run state available to expressions

Typical bindings (exact shape depends on flow):

| Binding | Description |
|---------|-------------|
| `input` | Caller payload from `/v1/evaluate` |
| `nodes` | Map of node id → that node's output state |
| `context` | Optional evaluate context (legacy envelope) |

After an `http` node runs:

```
nodes.http_fetch_user.status
nodes.http_fetch_user.body.fieldName
```

## Common patterns

### Condition

```json
"expression": "nodes.http_fetch_user.body.balance >= input.amount"
```

### HTTP input mapping

```json
"inputs": {
  "userId": "input.userId",
  "filter": "\"active\""
}
```

### Transform

```json
"expression": "{ \"fullName\": nodes.http_1.body.firstName & ' ' & nodes.http_1.body.lastName }"
```

### Decision with dynamic value

Prefix with `=` to evaluate as JSONata:

```json
"responseValues": {
  "approved": true,
  "reason": "=nodes.cond_1.expression ? 'auto-approved' : 'manual review'"
}
```

## Tips for AI agents

1. **Quote strings** inside JSONata correctly; the expression is a JSON string value.
2. Prefer **`input.field`** over deep paths when the start node declares `inputSchema`.
3. Reference HTTP bodies via **`nodes.{id}.body`**, not the raw response object.
4. Test complex expressions in the web console designer (Monaco + intellisense) before syncing.
5. Keep expressions **pure** — no side effects; runtime does not allow custom functions beyond JSONata standard.

## Intellisense

The console exposes TypeScript declarations for `input` and prior node outputs. After pulling flows, use the designer for expression authoring when possible.

Public API: `GET /v1/intellisense/...` (console-authenticated) generates `.d.ts` from the graph.
