JSON_TYPE()

Overview

Returns the type of an item of JSON data.

USAGE

JSON_TYPE(json_data)

Argument Name

Description

json_data

The JSON data to check

DETAILS

JSON_TYPE() is a JSON informational function that inspects valid JSON data and returns its data type.

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

The type returned is one of the following values:

  • ARRAY - a JSON array

  • OBJECT - a JSON object

  • STRING - a JSON string

  • INTEGER - an integer value (non-quoted)

  • DECIMAL - a floating point value (non-quoted)

  • BOOLEAN - a JSON boolean value of true or false

  • NULL - a JSON null value

Note that MariaDB Server returns DOUBLE instead of DECIMAL.

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

EXAMPLES

With Literal Values

In the following example, JSON_TYPE() is called with literal values:

SELECT JSON_TYPE('{"bar": "a string"}') AS type_1,
       JSON_TYPE('[1,2]') AS type_2,
       JSON_TYPE(1) AS type_3,
       JSON_TYPE(1.5) AS type_4,
       JSON_TYPE('"a"') AS type_5,
       JSON_TYPE('false') AS type_6,
       JSON_TYPE('null') AS type_7\G
*************************** 1. row ***************************
type_1: OBJECT
type_2: ARRAY
type_3: INTEGER
type_4: DECIMAL
type_5: STRING
type_6: BOOLEAN
type_7: NULL
SELECT JSON_TYPE(NULL);
+-----------------+
| JSON_TYPE(NULL) |
+-----------------+
| NULL            |
+-----------------+

Example Schema and Data

Some of the examples are based on the type_test table:

CREATE TABLE type_test (
  example JSON
);
INSERT INTO type_test VALUES
  ('{"bar": "hi", "foo": 42}'),
  ('[1, 2, 3]'),
  ('"Now is the time"');

Per-row Values

SELECT example, JSON_TYPE(example) AS 'type'
FROM type_test\G
*************************** 1. row ***************************
example: {"bar": "hi", "foo": 42}
   type: OBJECT
*************************** 2. row ***************************
example: [1, 2, 3]
   type: ARRAY
*************************** 3. row ***************************
example: "Now is the time"
   type: STRING

JSON Type at Specific Path

SELECT JSON_TYPE(JSON_EXTRACT(example, '$.bar')) as bar_type
FROM type_test;
+----------+
| bar_type |
+----------+
| STRING   |
| NULL     |
| NULL     |
+----------+

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