Skip to content

Returns a record with fields whose value is null removed.

drop_null_fields(x:record, field:field...) -> record

The drop_null_fields function returns a copy of x with every field whose value is null removed. Without explicit selectors, every field is considered, including fields nested inside record-typed values. With selectors, only the named fields (and any fields nested within them) are considered for removal; other null fields are preserved.

Unlike print_ndjson(strip_null_fields=true), the function never serializes the record, so secret-typed values flow through unchanged and reach the sink unredacted.

The record from which to remove null fields.

A comma-separated list of field paths inside x to consider. When omitted, every field is considered.

from {
id: 42,
status: "active",
comment: null,
}
this = drop_null_fields(this)
{
id: 42,
status: "active",
}

A common use case is cleaning optional null fields out of an inline body record before sending it to an API that rejects JSON null values:

from_http "https://api.example.com/v1/lookup",
method="POST",
body=drop_null_fields({
version: 1,
query: "example",
options: null,
})

The wire body contains only version and query; options is omitted.

from {
a: null,
b: null,
c: null,
d: 1,
}
this = drop_null_fields(this, a, c)
{
b: null,
d: 1,
}

b stays in the result because it was not named in the selector list, even though it is null.

from {
metadata: {
created: "2024-01-01",
updated: null,
},
data: {
value: 42,
comment: null,
},
}
this = drop_null_fields(this)
{
metadata: {
created: "2024-01-01",
},
data: {
value: 42,
},
}

Last updated: