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

  1. mysql> SELECT JSON_KEYS('{"a": 1, "b": {"c": 30}}');
  2. +-----------------------------------------------------+
  3. | json_keys(cast('{"a": 1, "b": {"c": 30}}' as JSON)) |
  4. +-----------------------------------------------------+
  5. | ["a", "b"] |
  6. +-----------------------------------------------------+
  7. 1 row in set (0.35 sec)
  8. mysql> SELECT JSON_KEYS('{"a": 1, "b": {"c": 30}}', '$.b');
  9. +------------------------------------------------------------+
  10. | json_keys(cast('{"a": 1, "b": {"c": 30}}' as JSON), '$.b') |
  11. +------------------------------------------------------------+
  12. | ["c"] |
  13. +------------------------------------------------------------+
  14. 1 row in set (0.07 sec)
  15. mysql> SELECT JSON_KEYS('{}');
  16. +-------------------------------+
  17. | json_keys(cast('{}' as JSON)) |
  18. +-------------------------------+
  19. | [] |
  20. +-------------------------------+
  21. 1 row in set (0.07 sec)
  22. mysql> SELECT JSON_KEYS('[1,2]');
  23. +----------------------------------+
  24. | json_keys(cast('[1,2]' as JSON)) |
  25. +----------------------------------+
  26. | NULL |
  27. +----------------------------------+
  28. 1 row in set (0.07 sec)
  29. mysql> SELECT JSON_KEYS('[]');
  30. +-------------------------------+
  31. | json_keys(cast('[]' as JSON)) |
  32. +-------------------------------+
  33. | NULL |
  34. +-------------------------------+
  35. 1 row in set (0.07 sec)

Keywords

json,json_keys