> ## Documentation Index
> Fetch the complete documentation index at: https://docs.usebruno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Response query

> Query and filter nested response data using dot notation, deep navigation, and array filtering

Call `res()` as a function to read values from the **HTTP response body** of the request you just sent. It works in [vars](https://docs.usebruno.com/testing/script/vars.md), [assertions](https://docs.usebruno.com/testing/tests/assertions.md), [scripts](https://docs.usebruno.com/testing/script/getting-started.md), and [tests](https://docs.usebruno.com/testing/tests/introduction.md).

You do **not** need to assign the body to a variable first. Bruno already has it. `res('customer.address.city')` is like a safer nested get on that body (similar to [`lodash.get()`](https://lodash.com/docs/#get)).

```javascript theme={null}
// After the request returns JSON, query paths on the body:
res('customer.address.city')  // → "bangalore"
```

> [!NOTE]
>
> Response query filtering (including `[?]` with filter functions and object predicates) works in both **Safe Mode** and **Developer Mode**.

## Sample response body

All examples below assume JSON returned (for example from `https://echo.usebruno.com` or your own API):

```json theme={null}
{
  "customer": {
    "address": {
      "city": "bangalore"
    },
    "orders": [
      {
        "id": "order-1",
        "items": [
          { "id": 1, "amount": 10 },
          { "id": 2, "amount": 20 }
        ]
      },
      {
        "id": "order-2",
        "items": [
          { "id": 3, "amount": 30 },
          { "id": 4, "amount": 40 }
        ]
      }
    ]
  }
}
```

In a test or post-response script against that response:

```javascript theme={null}
test("city is bangalore", function () {
  expect(res("customer.address.city")).to.equal("bangalore");
});

bru.setVar("firstAmount", res("customer.orders.items.amount[0]"));
```

## Query cheat sheet

| Query | Result |
| - | - |
| `res("customer.address.city")` | `"bangalore"` |
| `res("customer.orders.items.amount")` | `[10, 20, 30, 40]` |
| `res("customer.orders.items.amount[0]")` | `10` |
| `res("..items.amount")` | `[10, 20, 30, 40]` |
| `res("..amount")` | `[10, 20, 30, 40]` |
| `res("..items.amount[0]")` | `10` |
| `res("..items[0].amount")` | `10` |
| `res("..items[5].amount")` | `undefined` |
| `res("..id")` | `["order-1", 1, 2, "order-2", 3, 4]` |
| `res("customer.orders.foo")` | `undefined` |
| `res("..customer.foo")` | `undefined` |
| `res("..address")` | `[{ city: "bangalore" }]` |
| `res("..address[0]")` | `{ city: "bangalore" }` |

## Query styles

### Dot notation

Walk the object path. When a step is an array, Bruno collects matching values from every element:

```javascript theme={null}
res('customer.orders.items.amount')
// → [10, 20, 30, 40]
```

### Deep navigation (`..`)

Skip intermediate keys and find a property anywhere under the body:

```javascript theme={null}
res('..items.amount')
// → [10, 20, 30, 40]

res('..amount')
// → [10, 20, 30, 40]
```

### Array indexing

Pick one element by index (`[0]` is the first):

```javascript theme={null}
res('..items[0].amount')
// → 10

res('customer.orders.items.amount[0]')
// → 10
```

### Filter with a function

`[?]` keeps items where the function returns `true`. The argument is each object in that array:

```javascript theme={null}
res('customer.orders.items[?].amount', (item) => item.amount > 20)
// → [30, 40]

res('..items[?].amount', (item) => item.amount > 30)
// → [40]
```

### Filter with an object predicate

Match items by property equality (same as checking each field with `===`):

```javascript theme={null}
res('customer.orders.items[?]', { id: 2, amount: 20 })
// → [{ id: 2, amount: 20 }]

res('..items[?]', { id: 2, amount: 20 })
// → [{ id: 2, amount: 20 }]
```

### Map with a function

If the function returns a non-boolean value, Bruno treats it as a mapper:

```javascript theme={null}
res('..items..amount[?]', (amount) => amount + 10)
// → [20, 30, 40, 50]
```

## Common mistakes

* **Do not** wrap the sample JSON in `const data = { ... }` and expect `res()` to read that variable. `res()` always reads the **live response body**.
* Prefer `res('path')` for nested gets. Use `res.getBody()` when you need the whole object or normal JavaScript property access:

```javascript theme={null}
const body = res.getBody();
body.customer.address.city  // same as res('customer.address.city')
```
