Menu

MySQL JSON_TYPE(): Return Values, NULL, and Examples

MySQL JSON_TYPE() returns a utf8mb4 string describing the type of a JSON value.

Syntax

JSON_TYPE(json_value)

Return Values

For a valid non-NULL argument, JSON_TYPE() can return:

Return value Meaning
OBJECT JSON object
ARRAY JSON array
BOOLEAN JSON true or false
NULL JSON null
INTEGER Integer scalar
DOUBLE Floating-point scalar
DECIMAL Exact decimal scalar
DATETIME DATETIME or TIMESTAMP scalar
DATE DATE scalar
TIME TIME scalar
STRING Character string scalar
BLOB Binary scalar
OPAQUE Other internal scalar type

Basic Examples

SELECT
  JSON_TYPE('{"name": "Ada"}') AS object_type,
  JSON_TYPE('[1, 2, 3]') AS array_type,
  JSON_TYPE('"hello"') AS string_type,
  JSON_TYPE('true') AS boolean_type;

The result types are OBJECT, ARRAY, STRING, and BOOLEAN.

Number Types

SELECT
  JSON_TYPE('1') AS integer_type,
  JSON_TYPE('1.25') AS number_type;

Typical output is INTEGER and DOUBLE. MySQL can also report DECIMAL for exact-decimal scalar values.

SQL NULL vs JSON null

These two cases are different:

SELECT
  JSON_TYPE(NULL) AS sql_null,
  JSON_TYPE('null') AS json_null;
  • JSON_TYPE(NULL) returns SQL NULL.
  • JSON_TYPE('null') returns the string 'NULL', meaning the JSON null literal.

Inspecting a Nested Value

Use JSON_EXTRACT() with JSON_TYPE():

SET @doc = '{"user": {"id": 10, "roles": ["admin"]}}';

SELECT
  JSON_TYPE(JSON_EXTRACT(@doc, '$.user')) AS user_type,
  JSON_TYPE(JSON_EXTRACT(@doc, '$.user.id')) AS id_type,
  JSON_TYPE(JSON_EXTRACT(@doc, '$.user.roles')) AS roles_type;

This returns OBJECT, INTEGER, and ARRAY.

Invalid JSON

JSON_TYPE() expects a valid JSON value. Invalid JSON produces an error:

SELECT JSON_TYPE('hello');

The text hello is not a valid JSON string literal because JSON strings require quotes. Use:

SELECT JSON_TYPE('"hello"');

If you need to validate text first, use JSON_VALID().

Summary

  • JSON_TYPE() returns a string describing a JSON value’s type.
  • SQL NULL and JSON null are different cases.
  • Numeric values may be reported as INTEGER, DOUBLE, or DECIMAL.
  • Invalid JSON raises an error.
  • Combine JSON_TYPE() with JSON_EXTRACT() to inspect nested values.

See the MySQL 8.4 JSON attribute functions.