Skip to main content

Compact

Mutate Mezmo Compatible

Synopsis​

Removes null values, empty and whitespace-only strings, empty arrays, and empty objects recursively.

Schema​

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

Configuration​

The following fields are used to define the processor:

FieldRequiredDefaultDescription
valuesN-Array of values to be removed when found, in addition to empty fields
excluded_fieldsN-Array of field names to exclude from compaction
descriptionN-Explanatory note
ifN-Condition to run
ignore_failureNfalseSee Handling Failures
on_failureN-See Handling Failures
on_successN-See Handling Success
tagN-Identifier
disabledNfalseWhen true, the processor is skipped and the event continues to the next one. Lets you take a processor out of the path without removing its configuration

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.

A string of nothing but whitespace counts as empty. A field holding " " or a lone tab is dropped exactly as "" is, so a padded field that a parser left behind does not survive as a value that merely looks blank.

note

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

Examples​

Basic​

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"
}
}
}
}

Specific Values​

Remove empty fields and...

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

specific values:

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

Conditionals​

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"
}
}