Return the top-level keys of a JSON object as a set of text values.
JSON_OBJECT_KEYS(json)
## Overview Returns the top-level keys of a JSON object as a set of TEXT values, one row per key. As a set-returning function it is typically used in the FROM clause or with LATERAL to expand a JSON document into rows for downstream joins, counts, or pivots. Only the outermost object's keys are emitted; nested object keys are not traversed. For the binary-JSON variant, use JSONB_OBJECT_KEYS. ## Behavior - Returns one TEXT row per top-level key. - Returns zero rows (empty set) for an empty object ({}). - Returns NULL for NULL input. - Raises an error if the input is valid JSON but is not an object. - Key order matches the textual order of keys in the input document. - Does not recurse into nested objects or arrays. ## Compatibility - Matches the set-returning json_object_keys convention in widely adopted SQL implementations. - Parsing follows RFC 8259.
| Name | Type | Description |
|---|---|---|
json | Specifies a JSON object whose top-level keys are returned one per row. Returns NULL for NULL input. Raises an error if the value is valid JSON but is not an object. |
-- Return keys from a literal object
SELECT JSON_OBJECT_KEYS('{"name": "Alice", "age": 30, "active": true}');
-- Only top-level keys are listed; nested object keys are not flattened
SELECT JSON_OBJECT_KEYS('{"user": {"id": 1}, "role": "admin"}');
-- Use in the FROM clause as a set-returning function
SELECT k
FROM JSON_OBJECT_KEYS('{"a": 1, "b": 2, "c": 3}') AS k;
-- Realistic: inspect the top-level shape of variable payloads
SELECT event_id, JSON_OBJECT_KEYS(payload) AS key
FROM analytics.telemetry.events
WHERE event_date = DATE '2026-04-19';
-- Combine with COUNT to measure key diversity per row
SELECT event_id, COUNT(*) AS top_level_key_count
FROM analytics.telemetry.events, LATERAL JSON_OBJECT_KEYS(payload)
GROUP BY event_id;