MySQL JSON_TYPE() Function

In MySQL, the JSON_TYPE() function returns the type of a given JSON value.

JSON_TYPE() Syntax

Here is the syntax of the MySQL JSON_TYPE() function:

JSON_TYPE(json_value)

Parameters

json_value
Required. A JSON value.

Return value

The JSON_TYPE() function returns a utf8mb4 string that represents the type of the given JSON value. The JSON_TYPE() function will return one of the following values:

  • OBJECT: JSON object
  • ARRAY: JSON array
  • BOOLEAN: JSON boolean
  • NULL: JSON null value
  • INTEGER: MySQL TINYINT, SMALLINT, MEDIUMINT, INT and BIGINT type
  • DOUBLE: MySQL DOUBLE and FLOAT type
  • DECIMAL: MySQL DECIMAL and NUMERIC type
  • DATETIME: MySQL DATETIME and TIMESTAMP type
  • DATE: MySQL DATE type
  • TIME: MySQL TIME type
  • STRING: MySQL CHAR, VARCHAR, TEXT, ENUM and SET type
  • BLOB: MySQL BINARY, VARBINARY, BLOB and BIT type
  • OPAQUE: Other types

This function will return NULL if the argument is NULL.

MySQL will give an error if the parameter is not a valid JSON document. You can use JSON_VALID() to verify the JSON document.

JSON_TYPE() Examples

Here are some examples of JSON_TYPE().

Example 1

SELECT JSON_TYPE('true'), JSON_TYPE('null'), JSON_TYPE('"abc"');
+-------------------+-------------------+--------------------+
| JSON_TYPE('true') | JSON_TYPE('null') | JSON_TYPE('"abc"') |
+-------------------+-------------------+--------------------+
| BOOLEAN           | NULL              | STRING             |
+-------------------+-------------------+--------------------+

Example 2: Numbers

SELECT JSON_TYPE('1'), JSON_TYPE('1.23');
+----------------+-------------------+
| JSON_TYPE('1') | JSON_TYPE('1.23') |
+----------------+-------------------+
| INTEGER        | DOUBLE            |
+----------------+-------------------+

Example 3: Arrays

SELECT JSON_TYPE('[]'), JSON_TYPE('[1, 2]');
+-----------------+---------------------+
| JSON_TYPE('[]') | JSON_TYPE('[1, 2]') |
+-----------------+---------------------+
| ARRAY           | ARRAY               |
+-----------------+---------------------+

Example 4: Objects

SELECT JSON_TYPE('{}'), JSON_TYPE('{"x": 1}');
+-----------------+-----------------------+
| JSON_TYPE('{}') | JSON_TYPE('{"x": 1}') |
+-----------------+-----------------------+
| OBJECT          | OBJECT                |
+-----------------+-----------------------+