---
title: compact
description: "Remove null and empty-string values from a collection."
---

# compact

Remove null and empty-string values from a collection.

## Syntax

```knap
{{ value | compact }}
```

## Usage

- Whitespace-only strings are empty. The values `0` and `false` are preserved.
- For objects, matching properties are removed. Nested collections are preserved.
- Serialized arrays and objects produced by another filter are recognized automatically.

## Examples

### Array

```json title="Data"
{
  "items": [
    null,
    "",
    "one",
    0,
    false,
    "two"
  ]
}
```

```knap title="Template"
{{ items | compact }}
```

```md title="Output"
["one",0,false,"two"]
```

### Object

```json title="Data"
{
  "values": {
    "empty": "",
    "missing": null,
    "count": 0,
    "enabled": false
  }
}
```

```knap title="Template"
{{ values | compact }}
```

```md title="Output"
{"count":0,"enabled":false}
```

## Related filters

- [unique](/filters/unique)
