Skip to main content

Compact

Transform Mezmo Compatible

Synopsis

Removes null values, empty strings, empty arrays, and empty objects recursively.

Schema

compact:
- description: <text>
- if: <script>
- ignore_failure: <boolean>
- on_failure: <processor[]>
- on_success: <processor[]>
- tag: <string>
- values: <array>

Configuration

FieldRequiredDefaultDescription
descriptionN-Explanatory note
ifN-Condition to run
ignore_failureNfalseSee Handling Failures
on_failureN-See Handling Failures
on_successN-See Handling Success
tagN-Identifier
valuesN-Array of values to be removed when found, in addition to empty fields

Details

Empty fields include null values, empty strings, empty arrays [], and empty objects {}. This processor helps reduce the document size and improve readability by eliminating all the unnecessary fields.

note

The processor is particularly useful for cleaning up documents before storage or transmission, reducing storage space and network bandwidth usage.

Examples

Basic Usage

Remove empty fields from document...

{
"name": "document1",
"tags": [],
"metadata": {},
"description": "",
"status": null,
"value": 42
}
compact:
- description: "Remove empty fields"

resulting in only non-empty fields:

{
"name": "document1",
"value": 42
}

Nested Objects

Process nested objects recursively...

{
"user": {
"name": "John",
"settings": {
"theme": "",
"notifications": {},
"preferences": {
"language": "en"
}
},
"groups": []
}
}
compact:
- description: "Clean user data"

removing empty fields at all levels:

{
"user": {
"name": "John",
"settings": {
"preferences": {
"language": "en"
}
}
}
}

Remove Specific Values

Remove empty fields and...

{
"status": "pending",
"priority": "low",
"tags": ["draft", "review", ""],
"metadata": {}
}
compact:
- values: ["low", "draft"]

specific values:

{
"status": "pending",
"tags": ["review"]
}

Conditional Execution

Compact only when a condition is met...

{
"type": "user",
"data": {
"id": "123",
"roles": [],
"settings": {}
}
}
compact:
- if: "ctx.type == 'user'"

only user documents are compacted:

{
"type": "user",
"data": {
"id": "123"
}
}