Skip to main content
Skip to main content

JSON_KEYS

Json_keys

Description

Syntax

ARRAY<STRING> json_keys(JSON, [VARCHAR path])

Returns the keys from the top-level value of a JSON object as a JSON array, or, if a path argument is given, the top-level keys from the selected path. Returns NULL if any argument is NULL, the json_doc argument is not an object, or path, if given, does not locate an object. An error occurs if the json_doc argument is not a valid JSON document or the path argument is not a valid path expression

Note:

The result array is empty if the selected object is empty. If the top-level value has nested subobjects, the return value does not include keys from those subobjects.

Example


mysql> SELECT JSON_KEYS('{"a": 1, "b": {"c": 30}}');
+-----------------------------------------------------+
| json_keys(cast('{"a": 1, "b": {"c": 30}}' as JSON)) |
+-----------------------------------------------------+
| ["a", "b"] |
+-----------------------------------------------------+
1 row in set (0.35 sec)

mysql> SELECT JSON_KEYS('{"a": 1, "b": {"c": 30}}', '$.b');
+------------------------------------------------------------+
| json_keys(cast('{"a": 1, "b": {"c": 30}}' as JSON), '$.b') |
+------------------------------------------------------------+
| ["c"] |
+------------------------------------------------------------+
1 row in set (0.07 sec)

mysql> SELECT JSON_KEYS('{}');
+-------------------------------+
| json_keys(cast('{}' as JSON)) |
+-------------------------------+
| [] |
+-------------------------------+
1 row in set (0.07 sec)

mysql> SELECT JSON_KEYS('[1,2]');
+----------------------------------+
| json_keys(cast('[1,2]' as JSON)) |
+----------------------------------+
| NULL |
+----------------------------------+
1 row in set (0.07 sec)

mysql> SELECT JSON_KEYS('[]');
+-------------------------------+
| json_keys(cast('[]' as JSON)) |
+-------------------------------+
| NULL |
+-------------------------------+
1 row in set (0.07 sec)

Keywords

json,json_keys