MariaDB JSON_TYPE() Function
In MariaDB, JSON_TYPE() is a built-in function that returns the type of a given JSON value as a string.
MariaDB JSON_TYPE() Syntax
Here is the syntax for the MariaDB JSON_TYPE() function:
JSON_TYPE(json_value)
Parameters
json_value-
Required. A JSON value.
If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_TYPE'.
Return value
The MariaDB JSON_TYPE() function returns a utf8mb4 string representing the type of the given JSON value. the JSON_TYPE() function will return one of the following values:
ARRAY: JSON arrays, for example:[0,1,[2,3],{"x":1}].OBJECT: JSON objects, for example:{"x":1, "y":2}.BOOLEAN: JSON Boolean values, eg:true,false.NULL: JSONnullvalues, for example:null.DOUBLE: A number with at least one floating point decimal, eg:1.23.INTEGER: A number without floating point decimals, for example:1.STRING: JSON string, for example:abc.
This JSON_TYPE() function will return NULL if the argument is NULL.
MariaDB JSON_TYPE() Examples
Here are some common examples to show the usages of the Mariadb JSON_TYPE() function.
Example 1
SELECT JSON_TYPE('true'), JSON_TYPE('null'), JSON_TYPE('"abc"');
Output:
+-------------------+-------------------+--------------------+
| JSON_TYPE('true') | JSON_TYPE('null') | JSON_TYPE('"abc"') |
+-------------------+-------------------+--------------------+
| BOOLEAN | NULL | STRING |
+-------------------+-------------------+--------------------+Example 2: Numbers
SELECT JSON_TYPE('1'), JSON_TYPE('1.23');
Output:
+----------------+-------------------+
| JSON_TYPE('1') | JSON_TYPE('1.23') |
+----------------+-------------------+
| INTEGER | DOUBLE |
+----------------+-------------------+Example 3: Array
SELECT JSON_TYPE('[]'), JSON_TYPE('[1, 2]');
Output:
+-----------------+---------------------+
| JSON_TYPE('[]') | JSON_TYPE('[1, 2]') |
+-----------------+---------------------+
| ARRAY | ARRAY |
+-----------------+---------------------+Example 4: Object
SELECT JSON_TYPE('{}'), JSON_TYPE('{"x": 1}');
Output:
+-----------------+-----------------------+
| JSON_TYPE('{}') | JSON_TYPE('{"x": 1}') |
+-----------------+-----------------------+
| OBJECT | OBJECT |
+-----------------+-----------------------+NULL parameter
The MariaDB JSON_TYPE() function will return NULL if the argument is NULL.
SELECT JSON_TYPE(NULL);
Output:
+-----------------+
| JSON_TYPE(NULL) |
+-----------------+
| NULL |
+-----------------+Conclusion
In MariaDB, JSON_TYPE() is a built-in function that returns the type of a given JSON value as a string.