Skip to content

keys

Retrieves a list of field names from a record.

keys(x:record) -> list<string>

The keys function returns a list of strings containing all field names from the input record x.

The record whose field names you want to retrieve.

from {x: 1, y: "hello", z: true}
select field_names = this.keys()
{
field_names: ["x", "y", "z"],
}

Use keys to dynamically access record fields

Section titled “Use keys to dynamically access record fields”

You can combine keys with sorting and element access to dynamically select fields from records. For example, the following collects the values from the element with the first key alphabetically in each record:

from (
{foo: 10, bar: 20, baz: 30},
{foo: 100, bar: 200},
{baz: 300, qux: 400},
{},
)
summarize first_sorted_key = this[this.keys().sort().first()]?.collect()
{
first_sorted_key: [20, 200, 300],
}

Use keys to get distribution of available fields

Section titled “Use keys to get distribution of available fields”
from (
{foo: 10, bar: 20, baz: 30},
{foo: 100, bar: 200},
{baz: 300, qux: 400},
{},
)
select key = this.keys()
unroll key
top key
{name: "foo", count: 2}
{name: "bar", count: 2}
{name: "baz", count: 2}
{name: "qux", count: 1}

has, get

Last updated: