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 objectARRAY
: JSON arrayBOOLEAN
: JSON booleanNULL
: JSONnull
valueINTEGER
: MySQLTINYINT
,SMALLINT
,MEDIUMINT
,INT
andBIGINT
typeDOUBLE
: MySQLDOUBLE
andFLOAT
typeDECIMAL
: MySQLDECIMAL
andNUMERIC
typeDATETIME
: MySQLDATETIME
andTIMESTAMP
typeDATE
: MySQLDATE
typeTIME
: MySQLTIME
typeSTRING
: MySQLCHAR
,VARCHAR
,TEXT
,ENUM
andSET
typeBLOB
: MySQLBINARY
,VARBINARY
,BLOB
andBIT
typeOPAQUE
: 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 |
+-----------------+-----------------------+