Skip to main content
Version: 1.2.0

Compact

Mutate 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​

The following fields are used to define the processor:

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​

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