JSON_KEYS()

Overview

Returns the keys of a JSON object as a JSON array.

USAGE

JSON_KEYS(data[, path])

Argument Name

Description

data

The JSON data to query

path

Optional. The path specification of the desired object

DETAILS

JSON_KEYS() is a JSON function that extracts the key names from a valid JSON object.

The object to inspect is the top-level object unless the path argument is used to choose an object within the JSON data.

JSON data is valid if it conforms to ECMA-404: The JSON Data Interchange Standard.

A NULL is returned if either argument is NULL, if the indicated path is not found, or if the data being inspected is not a JSON object.

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

EXAMPLES

With Literal Values

In the following example, JSON_KEYS() is called with a JSON literal value. The result demonstrates how to obtain the keys in a JSON object:

SELECT JSON_KEYS('{"bar": 2, "baz": 3, "foo": 1}') AS result1,
       JSON_KEYS('[1, 2, 3, 4]') as result2,
       JSON_KEYS('[1, {"foo": 42}, 3]', '$[1]') as result3;
+-----------------------+---------+---------+
| result1               | result2 | result3 |
+-----------------------+---------+---------+
| ["bar", "baz", "foo"] | NULL    | ["foo"] |
+-----------------------+---------+---------+

Example Schema and Data

Some of the examples are based on the keys_test table:

CREATE TABLE keys_test (
  example JSON
);
INSERT INTO keys_test VALUES
  ('{"bar": {"deeper": [1, 2, 3]}, "foo": 42}'),
  ('{"other": 1, "results": 2}'),
  ('[3.14, {"deeper": "data"}, 9.2]');

Per-row Values

This example returns the key names of each row's JSON object as a JSON array. Note that it does not descend into any deeper objects:

SELECT JSON_KEYS(example)
FROM keys_test;
+----------------------+
| JSON_KEYS(example)   |
+----------------------+
| ["bar", "foo"]       |
| ["other", "results"] |
| NULL                 |
+----------------------+

JSON Keys at Specific Path

JSON_KEYS() may be used to find the object keys at a specific path in a JSON document by supplying the optional path argument:

SELECT JSON_KEYS(example, '$[1]')
FROM keys_test
WHERE example like '[%';
+----------------------------+
| JSON_KEYS(example, '$[1]') |
+----------------------------+
| ["deeper"]                 |
+----------------------------+

ERROR HANDLING

FEATURE INTERACTION

RESPONSES

DIAGNOSIS

ISO 9075:2016

CHANGE HISTORY

Release Series

History

23.09

  • Present starting in MariaDB Xpand 23.09.1.

6.1

  • Present starting in MariaDB Xpand 6.1.0.

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.1

  • Present starting in MariaDB Xpand 6.1.0.

EXTERNAL REFERENCES